본문 바로가기
Spring

Spring RestTemplate Sample

by 아이티.파머 2015. 9. 25.
반응형

간단하게 실행해보는 RestTemplate Sample



필요 jar

com.fasterxml.jackson.annotations.jar

commons-logging-1.2-javadoc.jar

commons-logging-1.2.jar

jackson-all-1.8.1.jar

spring-core-3.2.4.RELEASE.jar

spring-web-3.2.4.RELEASE.jar


* 일반 Client에서 사용시

import java.util.ArrayList;

import java.util.List;


import org.springframework.http.converter.HttpMessageConverter;

import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;

import org.springframework.web.client.RestTemplate;



public class HttpRestTest {

public static void main(String[] args) {

RestTemplate restTemplate = new RestTemplate();

String url = "https://api.github.com/users/mralexgray/repos";


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

MappingJacksonHttpMessageConverter map = new MappingJacksonHttpMessageConverter();

messageConverters.add(map);

restTemplate.setMessageConverters(messageConverters);


List<?> bean = restTemplate.getForObject(url, List.class);

System.out.println("The object received from REST call : " + bean);

}

}



* bean 등록후 사용시 xml

<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">

<property name="messageConverters">

<list>

<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>

</list>

</property>

</bean>




반응형