Java8 LocalDateTime to Joda LocalDateTime with jackson | 您所在的位置:网站首页 › date java › Java8 LocalDateTime to Joda LocalDateTime with jackson |
Java8 LocalDateTime to Joda LocalDateTime with jackson Yesterday I fought a problem that didn’t seem to take so much time that it actually did, so I decided to write a few lines to share the learned lesson. I had the following case: a spring boot rest service, that returns a JSON object, and a consumer that reads this object using RestTemplate. All great but the fact this object contains java.time.LocalDateTime in the service that had to be transformed to org.joda.time.LocalDateTime in the consumer. I decided not to implement custom deserializer and here is the other way I managed to do it work solving lots of org.springframework.http.converter.HttpMessageNotReadableException The serviceThe problem in the service was that the date in the result looked like "departureTimeUTC": { "month": "MARCH", "year": 2017, "dayOfMonth": 8, "dayOfWeek": "WEDNESDAY", "dayOfYear": 67, "hour": 2, "minute": 40, "nano": 0, "second": 0, "monthValue": 3, "chronology": { "id": "ISO", "calendarType": "iso8601" } }After adding the following maven dependency com.fasterxml.jackson.datatype jackson-datatype-jsr310And updating to false the write-dates-as-timestamps property in application.yml spring: jackson: serialization: write-dates-as-timestamps: falseGot the nicer result "departureTimeUTC": "2017-01-05T20:23:00"The clientIn the client the following dependency had to be added com.fasterxml.jackson.datatype jackson-datatype-jodaAnd then the RestTemplate had to “learn” about the JodaModule RestTemplate restTemplate = new RestTemplate();ObjectMapper objectMapper = new ObjectMapper();objectMapper.registerModule(new JodaModule());List |
CopyRight 2018-2019 实验室设备网 版权所有 |