간단하게 실행해보는 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>
'Spring' 카테고리의 다른 글
Spring RestTemplate / Connecter 예제 (0) | 2015.11.17 |
---|---|
Junit Test [A ServletContext is required to configure default servlet handling] (0) | 2015.11.02 |
대용량 페이징 기법 (0) | 2014.08.07 |
Spring initBinder - @vaild / JSR 303 (0) | 2014.04.07 |
spring / twitter 연동 (twitter4j) (4) | 2013.12.18 |