How to extract ObjectMapper from JAX-RS Client?

前端 未结 3 1488
逝去的感伤
逝去的感伤 2020-12-17 19:01

I am using Jersey JAX-RS client (version 2.0). I know it is using a Jackson ObjectMapper to generate and parse JSON. I want to use that same object to generate JSON for some

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-17 19:12

    Was not able to get to the jersey object mapper to configure it, based on the other solutions here (using jackson 2.8.3). (I suspect it may be related to osgi container, but nonetheless...) A brute-force way around it is to get the Response object from the Client and invoke your own instance of ObjectMapper on it. Then that same instance can be reused with your explicit configuration elsewhere as well.

    Client client = ClientBuilder.newClient();
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    ...
    Response r = client.target(URL).request().get();
    MyDtoClass dto = mapper.readValue((InputStream)(r.getEntity()), MyDtoClass.class);
    

提交回复
热议问题