Reputation: 6822
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!).
Upvotes: 1
Views: 1016
Reputation: 11992
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.
Upvotes: 1
Reputation: 35598
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).
Upvotes: 3