簡體   English   中英

Spring REST:帶有LocalDateTime的pojo錯誤請求

[英]Spring REST: Bad Request for pojo with LocalDateTime

Pojo.java

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

控制器:

@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());

content看起來像

{"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 Date&Time API數據類型。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM