Spring made it very easy to consume JSON webservices. In this article I describe how to receive and parse JSON and how to send your Java objects as JSON. First we need to include the required dependencies. If you use maven, include the following dependencies:

<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>3.1.2.RELEASE</version>
</dependency>
<dependency>
      <groupId>org.codehaus.jackson</groupId>
      <artifactId>jackson-mapper-asl</artifactId>
      <version>1.9.9</version>
</dependency>

Spring web has a RestTemplate class which can be used to call the REST webservices. The Jackson dependency supplies a message converter class which can be used to send and receive Java objects which are automatically converted to JSON en reversed from JSON.

//Create a Rest template
RestTemplate restTemplate = new RestTemplate();

//Create a list for the message converters

List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();

//Add the Jackson Message converter
messageConverters.add(new MappingJacksonHttpMessageConverter());

//Add the message converters to the restTemplate
restTemplate.setMessageConverters(messageConverters);

//A simple GET request, the response will be mapped to Example.class
Example result = restTemplate.getForObject(url, Example.class);

//Create an object which can be sent with the POST request
PostExample postRequest = new PostExample("value1", "value2");

//The POST request.
Example result2 = restTemplate.postForObject(url, postRequest, Example.class);

When the JSON response is for example:

{"example": {
      "id": "file",
      "value": "File"}
}

To convert these JSON to a class, it will look like:

public class PostExample {
      private String id;
      private String value;
}

When the JSON variable names are for example with a capital, you can use annotations to bind the correct values to your Java variables.

@JsonProperty("Name")
private String name;

When the JSON response contains more info than needed, you can use the class annotation JsonIgnoreProperties. When you set ignoreUnknown to true, all variables which are not defined in your class will be ignored.

@JsonIgnoreProperties(ignoreUnknown=true)
public class PostExample {}

When the root element is a list of Objects, you can simply use:

List resultList = Arrays.asList(restTemplate.getForObject(url, Example[].class));

JSON Sub object(s) When your JSON contains subobjects, you only have to create separate classes which have to be defined in the parent Java object. This can be a one to one, or one to many using a List. Alternatives Of course there are alternatives for Spring Web. GSON GSON is an easy to use Google library for parsing JSON strings to objects. It does only support parsing (Serializing and Deserializing), so you have to take care for the REST part. JAX-RS There are several JAX-RS implementations for consuming REST JSON interfaces. One of them is Jersey. It acts almost the same as the RestTemplate class.

shadow-left