java Null ModelAndView 返回给 DispatcherServlet,名称为“dispatcherServlet”:假设 HandlerAdapter 已完成请求处理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36029712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 00:53:33  来源:igfitidea点击:

Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling

javaweb-servicesrestiospring-boot

提问by Mohammed Javed

Hi Guys as this is my controller class when i run this application in postman adds-on its displaying status 200 ok.but file not reading how can i pass filename with extention as a string parameter in method? what iam missing ur assistance is highly appreciable and a knowledgeable

嗨,伙计们,这是我的控制器类,当我在邮递员附加组件中运行此应用程序时,其显示状态为 200 ok。但文件未读取如何在方法中将带有扩展名的文件名作为字符串参数传递?我所缺少的你的帮助是非常可观的和知识渊博的

@RequestMapping(value = "/file/{name}", method = RequestMethod.GET)
public ResponseEntity<InputStreamResource> download(@PathVariable String name) {
    try {
        File inputFile = fileSystemHandler.read(name);
        HttpHeaders headers = new HttpHeaders();
        // headers.add(HttpHeaders.CONTENT_TYPE, "application/octet-stream");
         headers.add(HttpHeaders.CONTENT_LENGTH, "" + inputFile.length());
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename= " + name);
        InputStreamResource isr = new InputStreamResource(new FileInputStream(inputFile));
        return new ResponseEntity<InputStreamResource>(isr, headers, HttpStatus.OK);
    } catch (Exception ex) {
        // return data;
    }
    return null;
}

And this is my File system handler class

这是我的文件系统处理程序类

public File read(String name) {
    File inputFile = null;
    try {
        inputFile = new File(env.getProperty("file.Path") + name);
        return inputFile;
    } catch (Exception ex) {
        Logger.getLogger(FileSystemHandler.class.getName()).log(Level.SEVERE, null, ex);
    }
    return inputFile;
}

采纳答案by Teemu Ilmonen

Question reads unclear, but I'm assuming that you are are requesting something like "readme.txt" but inside the request you are getting plain "readme" when checking filename. This is because spring tries to use the .txt at end of path to determine the content type of the response. You need to either disable that behaviour or use a trailing slashat end of your request (http://localhost:8080/file/readme.txt/).

问题读起来不清楚,但我假设您正在请求类似“readme.txt”的内容,但在请求中,您在检查文件名时得到的是简单的“readme”。这是因为 spring 尝试使用路径末尾的 .txt 来确定响应的内容类型。您需要禁用该行为或在请求结束时使用尾部斜杠( http://localhost:8080/file/readme.txt/)。

To disable in spring-boot you would do, something like this:

要在 spring-boot 中禁用,您可以执行以下操作:

@Configuration
public static class MvcConfig extends EnableWebMvcConfiguration {

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        super.configurePathMatch(configurer);
        configurer.setUseRegisteredSuffixPatternMatch(false);
        configurer.setUseSuffixPatternMatch(false);
    }
}