java - 如何修复 SpringBoot 中 RestController 中的转换器?
问题描述
我尝试做 POST 请求:
POST http://localhost:8080/api/add
Content-Type: application/json
{
"amount": "6",
"timestamp": "2021-01-31T14:35:01.00Z"
}
回应是:
HTTP/1.1 500
Content-Length: 0
Date: Sun, 31 Jan 2021 14:33:43 GMT
Connection: close
<Response body is empty>
Response code: 500; Time: 15ms; Content length: 0 bytes
有我的控制器:
@RestController
@RequestMapping(value = "/api")
public class StatsController {
private final StatisticService statisticsService;
@Autowired
public StatsController(StatisticService statisticsService) {
this.statisticsService = statisticsService;
}
@PostMapping(value = "/add")
public Transaction create(@RequestBody Transaction transaction) {
return statisticsService.createStats(transaction);
}
@GetMapping("/stats")
public Statistic getStats() {
return statisticsService.getStats();
}
}
但我也在 Spring Boot 中收到 WARN 消息:
2021-01-31 16:31:13.394 WARN 33984 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class java.util.LinkedHashMap]
2021-01-31 16:33:43.651 WARN 33984 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported]
我尝试了类似问题的不同建议:
- 检查我的 pojo 是否有 getter/setter
- 试图添加一些杰克逊依赖项
- 添加
consumes = MediaType.APPLICATION_JSON_VALUE
到 RestController 注释。 - 重新加载的maven项目
对我没有任何帮助。
解决方案
我已将 Spring Boot 版本从 2.4.2 更改为 2.4.0,问题已解决。
推荐阅读
- angular - 如何创建一个嵌套的递归表,可以在 Angular 中向下钻取 x 级别
- amazon-web-services - AWS API Gateway + Firehose 打包多条记录
- python - 从列表输出开始,将行附加到数据帧的 For 循环
- python - openpyxl - 处理超出“值不是无”的空单元格
- python - 混合生成器协程的消费/生产行为一定是个坏主意吗?
- python - ipywidgets IntSlider 的自定义格式
- docker - Websocket通过两个Nginx代理后得到504
- python-3.x - 如何用“无”字符串值替换无值
- ios - 自定义意图被一遍又一遍地确认,从未处理过
- r - 重命名环境 r 中的对象