开发者

How deserialize JSON object from HttpResponse using Jackson annotations?

开发者 https://www.devze.com 2023-03-25 04:43 出处:网络
I\'m using the Apache http classes to call a web service that returns a JSON object in the response body. I have a Jackson annotated java class mapped to the JSON object. I want to do something this,

I'm using the Apache http classes to call a web service that returns a JSON object in the response body. I have a Jackson annotated java class mapped to the JSON object. I want to do something this, but google hasn't turned up the correct boilerplate.

    String url = hostName + uri;
    HttpGet httpGet = new HttpGet(url);
    HttpResponse response = httpclient.e开发者_StackOverflowxecute(httpGet);
    MyObject myObject = (MyObject)response.getEntity().getContent();


You have to use the ObjectMapper:

MyObject myObject = objectMapper.readValue(response.getEntity().getContent(), MyObject.class);

(An object mapper instance can be reused, so no need to create a new one for each deserialization)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号