gpt4 book ai didi

java - Spring 休息: Bad Request for pojo with LocalDateTime

转载 作者:行者123 更新时间:2023-12-01 09:53:48 24 4
gpt4 key购买 nike

Pojo.java:

public class Pojo {
private LocalDateTime localDateTime;
private String message;
// Getters, setters, toString().

Controller :

@RequestMapping(value = "/test", method = RequestMethod.POST)
public ResponseEntity<?> test(@RequestBody Pojo pojo) {
return ResponseEntity.ok(pojo.toString());
}

集成测试:

Pojo pojo = new Pojo();
pojo.setMessage("message");
pojo.setLocalDateTime(LocalDateTime.now());
String content = jacksonObjectMapper.writeValueAsString(pojo);
this.mockMvc
.perform(post("/test").content(content).contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());

内容看起来像

{"localDateTime":{"dayOfMonth":23,"dayOfWeek":"MONDAY","dayOfYear":144,"monthValue":5,"hour":16,"minute":53,"nano":620000000,"second":6,"month":"MAY","year":2016,"chronology":{"id":"ISO","calendarType":"iso8601"}},"message":"message"}

测试失败,因为返回了 400 Bad Request。如果我在集成测试中评论 pojo.setLocalDateTime ... ,一切都会正常工作。

我该怎么做才能让Spring接受pojo中的LocalDateTime

最佳答案

包含 jackson-datatype-jsr310 数据类型模块,使 Jackson 能够识别 Java 8 日期和时间 API 数据类型。

关于java - Spring 休息: Bad Request for pojo with LocalDateTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37394456/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com