java - PUT 请求的 Spring Boot 406 状态码
问题描述
代码是
@RestController
@Component
@Slf4j
public class ServicesController {
@CrossOrigin
@PutMapping(
consumes = "multipart/form-data",
path = "/{id}/{route}/structure_article/{filename:.+}")
public ResponseEntity<ServiceResponse> updateStructureXMLFile(
@PathVariable("id") final String id,
@PathVariable("route") final String route,
@RequestParam("file") final MultipartFile uploadfile,
@PathVariable("filename") final String fileName) throws IOException {
(Some processing)
return new ResponseEntity<>(response, httpHeaders, HttpStatus.CREATED);
}
}
这里的响应是一个带有公共 getter 和 setter 的 POJO。 在此处输入图像描述
当我放置文件时出现此错误:
{
"timestamp": 1596783608973,
"status": 406,
"error": "Not Acceptable",
"exception": "org.springframework.web.HttpMediaTypeNotAcceptableException",
"message": "Could not find acceptable representation",
"path": "/7f3033d7-3979-45e0-9f0a-172b60568edb/articles/structure_article/manuscript.xml"
}
有什么办法可以解决这个问题?谢谢
解决方案
就我而言,内容协商的配置如下:
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer
.favorPathExtension(true)
.defaultContentType(MediaType.APPLICATION_JSON)
.mediaType("xml", MediaType.APPLICATION_XML)
.mediaType("json", MediaType.APPLICATION_JSON);
}
和你一样,我有一个控制器方法
@PutMapping("/user/{userId:.+}")
因此,Spring 尝试使用 Common MIME 类型中不存在的文件扩展名来处理请求/user/email@domain.com
,因此出现Content-Type
406错误。.com
我通过关闭来解决它favorPathExtension
。
推荐阅读
- sql-server - SQL Server 中的 T-SQL - 错误或功能?
- rest - 为什么将用户标识符存储在 HTTP 请求标头字段中被认为是无状态的,但将其存储在 URI 中却不是?
- css - 溢出的内容显示在焦点上
- python - Python 安装问题:我复制了 python3-config 以移动到所需的路径,但无法访问它
- javascript - 我收到 BadTokenException 或 InvalidDisplayException,在 WebView 中打开微调器时进行清理
- java - 通过接口自动装配 Spring Bean
- python - 突然的 Tensorflow / Keras Google Colab 依赖问题 `AttributeError: module 'tensorflow._api.v1.compat.v2' has no attribute '__internal__'`
- sql - 我们可以在 BigQuery 中自定义函数吗?如何在 BigQuery 中创建日期参数?
- asp.net-core - .Net Core 3.1 web api应用响应迟了,怎么办?
- sql - SQL插入无意中添加了多行