Calling REST Services using Spring’s REST Template

Hi guys, here is my 80th blog post which talks about calling Restful service using Spring’s “RestTemplate”. This codebase is executed under Spring 3.1 and 4.0 dependencies. The project structure looks as shown below.
RestTemplate_proj_struct

let me explain one scenario here, the calling REST service can return JSON Object or an XML Document back to consumer. it si up to the implementation how to convert JSON back to Java Object or convert Xml Back to Java.
We have many Marshallers and UnMarshallers available some of them are Jaxb2Marshaller,Jibx Marshaller, XStreamMarshaller,XmlBeanMarshaller..etc, i personally used Jaxb2Marshaller in this project. let us now dive into the code, there is no much explanation needed in the code, please follow the comments over the code blocks.

ConfigUtil.java

/**
 * 
 */
package com.spark.spring.rest.config;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.xml.MarshallingHttpMessageConverter;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.web.client.RestTemplate;

import com.spark.spring.rest.models.Customer;

/**
 * @author PavanKumar Mantha
 * 
 */
@Configuration
public class ConfigUtil {

	/**
	 * @return the RetTemplate Object. This method sets the marshaller object as
	 *         params to RestTemplate leveraging the marshalling capability to
	 *         the template.
	 */
	@Bean(name = "restTemplate")
	public RestTemplate getRestTemplate() {
		RestTemplate restTemplate = new RestTemplate();

		List<HttpMessageConverter<?>> converters = new ArrayList<>();
		converters.add(getMarshallingHttpMessageConverter());
		converters.add(new FormHttpMessageConverter());
		converters.add(new StringHttpMessageConverter());
		restTemplate.setMessageConverters(converters);
		return restTemplate;
	}

	/**
	 * @return MarshallingHttpMessageConverter object which is responsible for
	 *         marshalling and unMarshalling process
	 */
	@Bean(name = "marshallingHttpMessageConverter")
	public MarshallingHttpMessageConverter getMarshallingHttpMessageConverter() {

		MarshallingHttpMessageConverter marshallingHttpMessageConverter = new MarshallingHttpMessageConverter();
		marshallingHttpMessageConverter.setMarshaller(getJaxb2Marshaller());
		marshallingHttpMessageConverter.setUnmarshaller(getJaxb2Marshaller());
		return marshallingHttpMessageConverter;
	}

	/**
	 * @return Jaxb2Marshaller, this is the Jaxb2Marshaller object
	 */
	@Bean(name = "jaxb2Marshaller")
	public Jaxb2Marshaller getJaxb2Marshaller() {
		Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
		jaxb2Marshaller.setClassesToBeBound(Customer.class);
		return jaxb2Marshaller;
	}
}

RestServiceConsumer .java, RestService call

/**
 * 
 */
package com.spark.spring.rest.consume;

import org.springframework.web.client.RestTemplate;

import com.spark.spring.rest.models.Customer;

/**
 * @author PavanKumar Mantha
 *
 */
public class RestServiceConsumer {

	/**
	 * @param restTemplate, this is the exact call to the service
	 */
	public static void consumeService(RestTemplate restTemplate){
		// getting xml directly from the response as string
		String xml = restTemplate.getForObject("http://www.thomas-bayer.com/sqlrest/CUSTOMER/18/", String.class);
		System.out.println(xml);
		
		// unmarshalling xml string to java object using Jaxb
		Customer customer = restTemplate.getForObject("http://www.thomas-bayer.com/sqlrest/CUSTOMER/18/", Customer.class);
		System.out.println(customer.getFirstName());
	}

}

Customer.java, Jaxb mapping POJO

/**
 * 
 */
package com.spark.spring.rest.models;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * @author PavanKumar Mantha
 * 
 */
@XmlRootElement(name="CUSTOMER")
public class Customer {

	private String id;
	private String firstName;
	private String lastName;
	private String street;
	private String city;

	/**
	 * @return the id
	 */
	@XmlElement(name="ID")
	public String getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(String id) {
		this.id = id;
	}

	/**
	 * @return the firstName
	 */
	@XmlElement(name="FIRSTNAME")
	public String getFirstName() {
		return firstName;
	}

	/**
	 * @param firstName
	 *            the firstName to set
	 */
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	/**
	 * @return the lastName
	 */
	@XmlElement(name="LASTNAME")
	public String getLastName() {
		return lastName;
	}

	/**
	 * @param lastName
	 *            the lastName to set
	 */
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	/**
	 * @return the street
	 */
	@XmlElement(name="STREET")
	public String getStreet() {
		return street;
	}

	/**
	 * @param street
	 *            the street to set
	 */
	public void setStreet(String street) {
		this.street = street;
	}

	/**
	 * @return the city
	 */
	@XmlElement(name="CITY")
	public String getCity() {
		return city;
	}

	/**
	 * @param city
	 *            the city to set
	 */
	public void setCity(String city) {
		this.city = city;
	}

}

Main Method

/**
 * 
 */
package com.spark.spring.rest.test;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.web.client.RestTemplate;

import com.spark.spring.rest.config.ConfigUtil;
import com.spark.spring.rest.consume.RestServiceConsumer;

/**
 * @author PavanKumar Mantha
 *
 */
public class TestRestApp {

	/**
	 * @param args
	 */
	
	public static void main(String[] args) {
		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ConfigUtil.class);
		RestServiceConsumer.consumeService((RestTemplate)applicationContext.getBean("restTemplate"));
	}

}

finally when we run the main method you should see the below output on the screen.
console

Happy RESTing…(RestTemplate) 🙂