Search code examples
jsonspringrestpojo

Create POJO for consuming REST Web Service with Spring


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!).


Solution

  • 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).