java - 接收没有 Content-Type 标头的 RESTFUL 请求
问题描述
我正在练习弹簧靴并遇到问题。我有一个安静的 API 来接收来自其他 3rd 方的请求。我可以知道是否可以在没有 Content-Type 标头的情况下接收请求。由于没有 Content-Type=application/JSON 的请求,我一直面临错误 415-Unsupported Media Type。如果请求带有 ContentType 标头,问题就解决了。
@RequestMapping(value = "backend", method = RequestMethod.POST)
public ResponseEntity<String> Backend(@RequestBody RequestDto RequestDto) throws NoSuchAlgorithmException {
Service.process(RequestDto);
return ResponseEntity.ok("OK");
}
这是我的其余 API 代码。感谢你的帮助。
解决方案
感谢您的回复,并感谢所有试图帮助我的人。我从@daniu 发送的链接中找到了答案。Source :接收不带Content-Type header的请求 的解决方案 该解决方案不是使用springboot提供的@RequestBody,而是使用Gson手动将json字符串映射为object。
@RequestMapping(value = "backend", method = RequestMethod.POST)
public ResponseEntity<String> Backend(HttpEntity<String> json) throws NoSuchAlgorithmException {
ResponseDto ResponseDto = utils.gsonBuilder().fromJson(json.getBody(), ResponseDto.class);
BackendService.process(ResponseDto);
return ResponseEntity.ok("OK");
}
推荐阅读
- hyperlink - 如何在帖子中的 webflow 上插入 Link nofollow
- node.js - Node.JS 和 raw-socket 似乎不尊重源 IP
- google-apps-script - 谷歌应用脚本计数器功能
- ubuntu - 必须启用安全管理员才能远程访问 DAS - 访问 Glassfish 配置页面
- mysql - Docker使用单个Dockerfile运行多个图像?
- ssh - 选择:ssh localforward 上的参数无效
- swift - Swift 性能 segue 和导航控制器
- css - 动态呈现图像多行多列
- java - tomcat 8突然没反应
- image-processing - 具有非常小的图像数据集的更快的 R-CNN 模型