Spring URI PathVariable,文件扩展名无效

时间:2015-05-04 00:33:26

标签: spring spring-mvc file-upload content-negotiation

我在解决为什么在调用我的控制器方法之前剥离以下请求中的文件扩展名(.jpg)时遇到了问题:

GET http://blah.com/assets/picture.jpg

我已将内容协商设置为不支持路径扩展:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.blah" })
public class PlatformWebAppConfig extends WebMvcConfigurerAdapter {    

    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {    
        configurer.favorPathExtension(false);
        configurer.favorParameter(false);
        configurer.useJaf(false);
        configurer.defaultContentType(MediaType.APPLICATION_JSON);
    }
}

我的控制器是:

@Controller
public class FileUploadRestApi {
    @RequestMapping(
        value = "/assets/{filename}",
        method = RequestMethod.GET)
    public void downloadFile(HttpServletResponse response,
        @PathVariable("filename") String filename,
        Principal principal) {
            // ERROR: 'filename' has extension stripped !!!!
    }
}

我还尝试将以下内容添加到上面的PlatformWebAppConfig课程中,但没有运气:

@Override
public void configurePathMatch(PathMatchConfigurer matcher) {
    matcher.setUseSuffixPatternMatch(true);
    matcher.setUseRegisteredSuffixPatternMatch(true);
}

我已尝试调试此问题,并且我已经看到ContentNegotiationManagerFactoryBean.afterPropertiesSet肯定会将favorPathExtension设置视为错误并且未设置内容协商策略(如预期的那样)。

更新

如果我将请求映射更改为

"/assets/{filename:.+}"

文件名将包含扩展名,但这并不能解释为什么设置所有这些配置器没有实现任何目的????

0 个答案:

没有答案
相关问题