1

I have to consume a lot of JSON-based RESTful WebServices as part of my job as a developer on a Spring MVC application. We use RestTemplate and its great, but all the tutorials I see for this type of thing involve writing a POJO to mimic a request and response including all of its nested objects. This is a cumbersome process and I was wondering what alternatives there are.

My current workflow is to get JSON examples from the 3rd-party REST provider's documentation and plug this into a website like jsonSchema2Pojo (http://www.jsonschema2pojo.org/) which works ok, although it's only as effective as the third parties documentation (which is often lousy!).

IcedDante
  • 6,145
  • 12
  • 57
  • 100

2 Answers2

3

The alternative to creating POJOs for each request/response is to use a JSON library like Jackson, and utilize the generic JSON data structures (e.g JsonNode). An example is here: RestTemplate and acessing json

The result is that you'll have to access the JSON as if its a set of key/value pairs (where some keys contain other sets of key/value pairs).

Community
  • 1
  • 1
Chris Thompson
  • 35,167
  • 12
  • 80
  • 109
1

Besides pojo's and JsonNode, that Chris mentions, you can always just serialize the JSON response to a Map<Object, Object> or use Gson and it's JsonObject, which works similar to JsonNode.

One thing that a JsonNode and JsonObject has that a Map<Object, Object> does not is that they can let you easily extract various types of common data types from the JSON.

Jose Martinez
  • 11,452
  • 7
  • 53
  • 68