如何在springboot中修复restcontroller中的转换器?

ql3eal8s  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(325)

我试着做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中也收到了警告信息:

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 重新设置控制器注解。
重新加载的maven项目
什么也帮不了我。

6tqwzwtp

6tqwzwtp1#

我已经将SpringBoot版本从2.4.2改为2.4.0,问题已经解决。

相关问题