简体   繁体   English

尝试在 Spring Boot REST API 中下载文件时如何修复“找不到资源”?

[英]how to fix 'resource not found' when trying to download file in spring boot REST API?

I'm new to spring framework and REST, and now trying to migrate REST from jersey to spring boot 2.1 The controller works fine with jax-rs, however, I don't want to use jax-rs in spring boot.我是 spring 框架和 REST 的新手,现在尝试将 REST 从 jersey 迁移到 spring boot 2.1 控制器与 jax-rs 一起工作正常,但是,我不想在 spring boot 中使用 jax-rs。 So, I tried spring Mvc and I get 'resource not found' error.因此,我尝试了 spring Mvc,但出现“找不到资源”错误。 Please I'd really appreciate any help.请我真的很感激任何帮助。

I tried this我试过这个

@GetMapping(value ="/generic/download_file/{path:[^\\.+]*}", consumes ="application/vnd.X-FileContent")
    public ResponseEntity<?> downloadFile(@PathVariable("path") String filePath){

        String actualFilePath = "";

        try {

            actualFilePath = filePath.replaceAll("\\/", "\\\\");

            File file = new File(actualFilePath);

            if (file.exists()) {

                return ResponseEntity.ok().header("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"").body(file);

            } else {

                return errorHandling.errorResponseFactory("1.0.0", Thread.currentThread().getStackTrace()[1], "",
                        RecommendedSolution.UseValidDirectoryPath, "File not exist.");

            }

        } catch (Exception ex) {

            ActionLog.writeLog("program_library_v510", "1.0.0", "Exception occur during gettig generic package file",
                    ActionLogType.DebugLog);
            ActionLog.writeLog("program_library_v510", "1.0.0", "Exception occur during getting generic package file",
                    ActionLogType.ErrorLog);

            return errorHandling.errorResponseFactory("1.0.0", Thread.currentThread().getStackTrace()[1], "",
                    RecommendedSolution.UnexpectedErrorMsg, "");
        }
    }

2019-01-07 17:17:23.930 INFO 13664 --- [nio-9090-exec-2] osweb.servlet.DispatcherServlet : Completed initialization in 10 ms 2019-01-07 17:17:23.947 DEBUG 13664 --- [nio-9090-exec-2] osweb.servlet.DispatcherServlet : GET "/packages/download_file/D:/xfolder/test.txt", paramete rs={} 2019-01-07 17:17:24.002 DEBUG 13664 --- [nio-9090-exec-2] oswshandler.SimpleUrlHandlerMapping : Mapped to ResourceHttpRequestHandler ["classpath:/META-INF/ resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/", "/"] 2019-01-07 17:17:24.006 DEBUG 13664 --- [nio-9090-exec-2] oswsrResourceHttpRequestHandler : Resource not found 2019-01-07 17:17:24.007 DEBUG 13664 --- [nio-9090-exec-2] osweb.servlet.DispatcherServlet : Completed 404 NOT_FOUND 2019-01-07 17:17:24.015 DEBUG 13664 --- [nio-9090-exec-2] osweb.servlet.DispatcherServlet : "ERROR" dispatch for GET "/error", parameters={} 2019-01-07 17:17:24.029 DEBUG 13664 --- [nio-9090-exec-2] swsmmaRequestMappingHandlerMapping : Mapped to p 2019-01-07 17:17:23.930 INFO 13664 --- [nio-9090-exec-2] osweb.servlet.DispatcherServlet:在 10 毫秒内完成初始化 2019-01-07 17:17:23.947 DEBUG 1 [nio-9090-exec-2] osweb.servlet.DispatcherServlet : GET "/packages/download_file/D:/xfolder/test.txt", 参数 rs={} 2019-01-07 17:17:24.002 DEBUG 13664 - -- [nio-9090-exec-2] oswshandler.SimpleUrlHandlerMapping : 映射到 ResourceHttpRequestHandler ["classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath: /public/", "/"] 2019-01-07 17:17:24.006 DEBUG 13664 --- [nio-9090-exec-2] oswsrResourceHttpRequestHandler :找不到资源 2019-01-07 17:17:24.0136 DEBUG --- [nio-9090-exec-2] osweb.servlet.DispatcherServlet : Completed 404 NOT_FOUND 2019-01-07 17:17:24.015 DEBUG 13664 --- [nio-9090-exec-2] osweb.servlet.DispatcherServlet :GET“/错误”的“错误”调度,参数={} 2019-01-07 17:17:24.029 DEBUG 13664 --- [nio-9090-exec-2] swsmmaRequestMappingHandlerMapping :映射到 p ublic org.springframework.http.ResponseEntity> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServ letRequest) 2019-01-07 17:17:24.077 DEBUG 13664 --- [nio-9090-exec-2] oswsmmaHttpEntityMethodProcessor : Using 'application/json', given [ / ] and supported [applic ation/json, application/ +json, application/json, application/ +json] 2019-01-07 17:17:24.078 DEBUG 13664 --- [nio-9090-exec-2] oswsmmaHttpEntityMethodProcessor : Writing [{timestamp=Mon Jan 07 17:17:24 SGT 2019, status=40 4, error=Not Found, message=No message available, path=/packages/download_file/D:/xfolder/test.txt}] 2019-01-07 17:17:24.146 DEBUG 13664 --- [nio-9090-exec-2] osweb.servlet.DispatcherServlet : Exiting from "ERROR" dispatch, status 404 ublic org.springframework.http.ResponseEntity> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServ letRequest) 2019-01-07 17:17:24.077 DEBUG 13664 -- - [nio-9090-exec-2] oswsmmaHttpEntityMethodProcessor : 使用'application/json',给定[ / ] 并支持[application/json, application/ +json, application/json, application/ +json] 2019-01-07 17:17:24.078 DEBUG 13664 --- [nio-9090-exec-2] oswsmmaHttpEntityMethodProcessor : 写作 [{timestamp=Mon Jan 07 17:17:24 SGT 2019, status=40 4, error=Not Found, message=No消息可用,路径=/packages/download_file/D:/xfolder/test.txt}] 2019-01-07 17:17:24.146 DEBUG 13664 --- [nio-9090-exec-2] osweb.servlet.DispatcherServlet :退出“错误”调度,状态 404

I suspect two things here.我怀疑这里有两件事。 Let me clarify this.让我澄清一下。

  1. What is the API url you are calling?你调用的 API url 是什么? /generic/download_file/{path:[^\\\\.+]*} or /packages/download_file/D:/xfolder/test.txt /generic/download_file/{path:[^\\\\.+]*}/packages/download_file/D:/xfolder/test.txt

both are looks different.两者看起来都不一样。 Please see generic and packages请参阅通用

  1. The better way to pass filenames in URL is using @RequestParam instead of @PathVariable在 URL 中传递文件名的更好方法是使用@RequestParam而不是@PathVariable

    @GetMapping(value ="/generic/download_file/", consumes ="application/vnd.X-FileContent") public ResponseEntity downloadFile(@RequestParam("path") String filePath){ @GetMapping(value ="/generic/download_file/", 消费 ="application/vnd.X-FileContent") public ResponseEntity downloadFile(@RequestParam("path") String filePath){

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM