1

I need to parse the jax-ws rest response and I tried the following two ways of parsing the response.Both works good.But I am in need to know the best efficient way of implementation.Please provide me your view.

First Approach:

  • Use getEntity Object and get the response as Input Stream.
  • Using Jackson ObjectMapper readValue() -covert the inputstream to java object.
  • Using getters and setters of nested java class get the response objects member values.

Second Approach:

  • Use getEntity Object and get the response as Input Stream and and convert the Input Stream to String.
  • Using Google Json API,convert the string to json object.
  • Using Json parser and get the nested objects member values.
satyanehru
  • 11
  • 3

2 Answers2

0

I would say the first approach is better for two reasons:

  1. You don't go through the intermediate process of reading the response payload into String
  2. The setter methods that are called during Jackson deserialization may perform validation on input and throw appropriate exceptions, so you do validation during deserialization.
Sharon Ben Asher
  • 13,849
  • 5
  • 33
  • 47
0

Maybe not a general answer to this question but another variant of what you're describing under "First approach". I would start with a generic data structure and would only introduce an extra bean if necessary. I wouldn't use String to pass structured data around.

Use jackson to convert the JSON response to a Map<String,Object> or JsonNode.

Advantage:

  • You don't need to implement a specialized bean class. Even a very simple bean can become unhandy over time (if format changes or new nested structures are added to the json response, etc.). It also introduces some kind of metaphor to your code which sometimes helps but also can be misleading.
  • Map<String,Object> is in the JDK and offers a good interface to access data. You don't have to change any interfaces even if the JSON format changes. You can always pass your data in form of a Map<String,Object>

Disadvantage

  • Data Encapsulation. The map is a very close representation of the input data and therefore offers not same level of abstraction like a bean.
Community
  • 1
  • 1
jschnasse
  • 8,526
  • 6
  • 32
  • 72