RestTemplate Java Consumer Example
RestTemplate Java Consumer Example

RestTemplate Java Consumer Example

In this tutorial you will see how to consume RESTFul service using java and here I will show you two way to consume RESTFul web service i.e if you are consume service inside corporate proxy or without proxy:

  • Project structure:

  • pom.xml:
<!-- ~ Copyright 2009 the original author or authors. ~ ~ Licensed under 
	the Apache License, Version 2.0 (the "License"); ~ you may not use this file 
	except in compliance with the License. ~ You may obtain a copy of the License 
	at ~ ~ http://www.apache.org/licenses/LICENSE-2.0 ~ ~ Unless required by 
	applicable law or agreed to in writing, software ~ distributed under the 
	License is distributed on an "AS IS" BASIS, ~ WITHOUT WARRANTIES OR CONDITIONS 
	OF ANY KIND, either express or implied. ~ See the License for the specific 
	language governing permissions and ~ limitations under the License. -->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.springsource.samples</groupId>
	<artifactId>resttemplate</artifactId>
	<packaging>jar</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>Spring 3 RestTemplate Sample</name>
	<repositories>
		<repository>
			<id>s2-snapshot</id>
			<url>http://repository.springsource.com/maven/bundles/snapshot</url>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
		</repository>
		<repository>
			<id>s2-release</id>
			<url>http://repository.springsource.com/maven/bundles/release</url>
		</repository>
		<repository>
			<id>s2-external</id>
			<url>http://repository.springsource.com/maven/bundles/external</url>
		</repository>
	</repositories>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.5</source>
					<target>1.5</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>4.1.4.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.ws</groupId>
			<artifactId>org.springframework.xml</artifactId>
			<version>1.5.5.A</version>
		</dependency>
		<dependency>
			<groupId>org.apache.log4j</groupId>
			<artifactId>com.springsource.org.apache.log4j</artifactId>
			<version>1.2.15</version>
		</dependency>
		<dependency>
			<groupId>com.google.code.gson</groupId>
			<artifactId>gson</artifactId>
			<version>2.8.0</version>
		</dependency>
		<dependency>
			<groupId>com.google.guava</groupId>
			<artifactId>guava</artifactId>
			<version>19.0</version>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-core</artifactId>
			<version>2.8.5</version>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.8.5</version>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-annotations</artifactId>
			<version>2.8.5</version>
		</dependency>
	</dependencies>
</project>
  • applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="crunchAPIWithPass" class="com.java.resttemplae.test.CrunchAPIWithPass">
        <constructor-arg ref="restTemplate"/>
    </bean>
    
    <bean id="cruncAPIWithouPass" class="com.java.resttemplae.test.CruncAPIWithouPass">
        <constructor-arg ref="restTemplate"/>
    </bean>

    <bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
        <property name="messageConverters">
            <list>
                <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/>
            </list>
        </property>
    </bean>

</beans>
  • CruncAPIWithouPass.java:
package com.java.resttemplae.test;

import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

public class CruncAPIWithouPass {

    private final RestTemplate restTemplate;

    public CruncAPIWithouPass(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }
    
    public void getDataUsingRestTemplate() {
       
        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
    	String url = "https://api.crunchbase.com/v/3/odm-organizations?user_key=38bfc645dcb86c44bb5fbec1984ba943";
    	
    	//option 1
    	Object forNow  = restTemplate.getForObject(url, Object.class);
    	System.out.println(forNow.toString());
    	
    	//option 2
    	ResponseEntity<Object> value = restTemplate.exchange(url, HttpMethod.GET,null, Object.class);
    	System.out.println(value.getBody().toString());
    }
    
}
  • CrunchAPIWithPass.java:
package com.java.resttemplae.test;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Proxy.Type;

import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

public class CrunchAPIWithPass {

    private final RestTemplate restTemplate;

    public CrunchAPIWithPass(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }
    
    public void getDataUsingRestTemplate() {
    	
    	SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();

        Proxy proxy= new Proxy(Type.HTTP, new InetSocketAddress("proxyname", 80));
        requestFactory.setProxy(proxy);
        
        restTemplate.setRequestFactory(requestFactory);
    	
        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
    	String url = "https://api.crunchbase.com/v/3/odm-organizations?user_key=38bfc645dcb86c44bb5fbec1984ba943";
    	
    	//option 1
    	Object object  = restTemplate.getForObject(url, Object.class);
    	System.out.println(object.toString());
    	
    	//option 2
    	ResponseEntity<Object> value = restTemplate.exchange(url, HttpMethod.GET,null, Object.class);
    	System.out.println(value.getBody().toString());

    }

}
  • Application.java:
package com.java.resttemplae.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Application {

    public static void main(String[] args) {

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        
        //CruncAPIWithouPass client = applicationContext.getBean("cruncAPIWithouPass", CruncAPIWithouPass.class);
        CrunchAPIWithPass client = applicationContext.getBean("crunchAPIWithPass", CrunchAPIWithPass.class);
        
        client.getDataUsingRestTemplate();
        
        ((AbstractApplicationContext)applicationContext).close();
    }

}

Leave a Reply

Your email address will not be published. Required fields are marked *