Registering JacksonJsonProvider with ObjectMapper + JavaTimeModule to Jersey 2 Client

后端 未结 3 1776
没有蜡笔的小新
没有蜡笔的小新 2021-02-04 09:46

I\'m trying to marshal response containing ISO formatted timestamp like that:

{
...
    \"time\" : \"2014-07-02T04:00:00.000000Z\"
...
}

into <

3条回答
  •  有刺的猬
    2021-02-04 10:26

    Jackson configuration looks fine, I tried the following and was able to deserialize the value:

    public class Test {
    
        public static void main(String[] args) throws Exception {
            ObjectMapper mapper = new ObjectMapper();
            mapper.registerModule(new JavaTimeModule());
            Model model = mapper.readValue("{\"time\" : \"2014-07-02T04:00:00.000000Z\"}", Model.class);
            System.out.println(model.getTime());
        }
    
    }
    
    class Model{
        private ZonedDateTime time;
    
        public ZonedDateTime getTime() {
            return time;
        }
        public void setTime(ZonedDateTime time) {
            this.time = time;
        }
    }
    

    I can reproduce it by commenting out mapper.registerModule(new JavaTimeModule());. So, it looks like jersey client is not using custom mapper instance. Could you try configuring it as described here.

提交回复
热议问题