Wednesday, May 1, 2013

Spring Jackson library example to consume JSON returned by REST service

A brief example of how to use the Spring Jackson library to consume a JSON returned by a REST service. The class below is a Singleton that initializes the RestTemple one time only.

import java.util.ArrayList;
import java.util.List;
import org.springframework.http.MediaType;
import org.springframework.http.client.CommonsClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

public class RestJsonTest {
 private static RestJsonTest instance;
 private RestTemplate restTemplate;
 private String url ="http://rest.service.url";
 public static RestJsonTest getInstance() {
  if (instance == null) {
   instance = new RestJsonTest();
  return instance;
 private RestJsonTest() {
  // Setup the RestTemplate configuration.
  restTemplate = new RestTemplate();
  restTemplate.setRequestFactory(new CommonsClientHttpRequestFactory());
  List<HttpMessageConverter<?>> messageConverterList = restTemplate.getMessageConverters();
  // Set HTTP Message converter using a JSON implementation.
  MappingJacksonHttpMessageConverter jsonMessageConverter = new MappingJacksonHttpMessageConverter();
  // Add supported media type returned by BI API.
  List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
  supportedMediaTypes.add(new MediaType("text", "plain"));
  supportedMediaTypes.add(new MediaType("application", "json"));
 public SearchResults searchResults() {
  return restTemplate.getForObject(url, SearchResults.class);  
 public static void main(String[] args) {
  RestJsonTest jsonTest = RestJsonTest.getInstance();
  SearchResults results = jsonTest.searchResults();

The mapping of the JSON to Java classes can be done via annotations like it's shown here:
package com.bodybuilding.api.commerce.clientservice;

import java.util.List;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;

 * "search_keywords":"Social Networks",
 *  "total_time":200,
 *  "results":{
 *   "result_01":{
 *    "url":"",
 *    "rank": "1"
 *   },
 *   "result_02":{
 *   "url":"",
 *    "rank": "2"
 *   }
 *  }
 * }
public class SearchResults {
 private String keywords;
 private long totalTime;
 private List<SearchResult> results;
 public String getKeywords() {
  return keywords;
 public void setKeywords(String keywords) {
  this.keywords = keywords;
 public long getTotalTime() {
  return totalTime;
 public void setTotalTime(long totalTime) {
  this.totalTime = totalTime;
 public List<SearchResult> getResults() {
  return results;
 public void setResults(List<SearchResult> results) {
  this.results = results;

import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;

public class SearchResult {
 private String url;
 private int rank;
 public String getUrl() {
  return url;
 public void setUrl(String url) {
  this.url = url;
 public int getRank() {
  return rank;
 public void setRank(int rank) {
  this.rank = rank;

The RestTemplate bean can be also configure with Spring injection:

<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
     <property name="requestFactory">
      <bean id="clientHttpRequestFactory" class="org.springframework.http.client.CommonsClientHttpRequestFactory" />   
     <property name="messageConverters">
       <bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
        <property name="supportedMediaTypes">
          <bean id="jsonMediaTypeTextPlain" class="org.springframework.http.MediaType">
           <constructor-arg value="text"/>
          <constructor-arg value="plain"/>
          <bean id="jsonMediaTypeApplicationJson" class="org.springframework.http.MediaType">
           <constructor-arg value="application"/>
          <constructor-arg value="json"/>

No comments:

Post a Comment