Java @PathVariable文件名到406的结果不可接受
我有一个如下所示的处理程序方法,它接受Java @PathVariable文件名到406的结果不可接受,java,spring,spring-mvc,spring-boot,kotlin,Java,Spring,Spring Mvc,Spring Boot,Kotlin,我有一个如下所示的处理程序方法,它接受文件名 @GetMapping(value = ["/test/{fileName:.+}"], produces = [(MediaType.APPLICATION_JSON_VALUE)]) fun getFile(@PathVariable fileName: String): ResponseEntity<Map<String, String>> { return ResponseEntity.ok(hashMapOf
文件名
@GetMapping(value = ["/test/{fileName:.+}"], produces = [(MediaType.APPLICATION_JSON_VALUE)])
fun getFile(@PathVariable fileName: String): ResponseEntity<Map<String, String>> {
return ResponseEntity.ok(hashMapOf(Pair("fileName", fileName)))
}
如何解决这个问题?我希望能够使用有效的扩展名调用该方法。这是因为内容协商,只需尝试将
内容类型添加到请求头,它就可以解决此问题,或者禁用内容协商。查看此链接:回答给了我需要做什么的提示。这是我找到的一个解决方案
解决方案1:配置ContentNegotiation
@Bean
public WebMvcConfigurerAdapter webMvcConfigurerAdapter() {
return new WebMvcConfigurerAdapter() {
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer
.useJaf(false)
.defaultContentType(MediaType.APPLICATION_JSON);
}
};
}
解决方案2:编辑application.yml/application.properties
spring:
mvc:
media-types:
png: application/json
jpg: application/json
jpeg: application/json
使用弹簧靴1.5.9.1释放
spring:
mvc:
media-types:
png: application/json
jpg: application/json
jpeg: application/json