sitelink1 http://knight76.tistory.com/entry/Spring...8%EC%A0%9C 
sitelink2  
sitelink3  
extra_vars6  

Spring RestTemplate을 사용하는 예제이다. 

 

API 서버와 json 통신(utf)을 하는 예제로서, 결과 값을 List<Object>로 변환하는 예제이다. 

 

한글 정보를 잘 받기 위해 StringHttpMessageConverter 을 잘 활용한다. 한글 깨짐없이 문제 없이 동작한다. 

 

 

https://gist.github.com/knight1128/b0e545a03e2d066da8f6#file-resttemplateintegrationtest

 

 

package com.google.locationlab;

 

import com.google.common.collect.Lists;

import com.google.locationlab.model.LocationResponse;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import org.junit.Test;

import org.springframework.core.ParameterizedTypeReference;

import org.springframework.http.*;

import org.springframework.http.converter.HttpMessageConverter;

import org.springframework.http.converter.StringHttpMessageConverter;

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

import org.springframework.web.client.RestTemplate;

import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;

import java.nio.charset.Charset;

import java.util.List;

 

public class RestTemplateIntegrationTest {

private Log logger = LogFactory.getLog(RestTemplateIntegrationTest.class);

 

@Test

public void test() throws Exception {

RestTemplate restTemplate = new RestTemplate();

 

MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();

HttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(Charset.forName("UTF-8"));

 

List<HttpMessageConverter<?>> httpMessageConverter = Lists.newArrayList();

httpMessageConverter.add(mappingJackson2HttpMessageConverter);

httpMessageConverter.add(stringHttpMessageConverter);

restTemplate.setMessageConverters(httpMessageConverter);

 

URI targetUrl = UriComponentsBuilder.fromUriString("http://location-api.google.com")

.path("search")

.queryParam("text", "잠실역")

.build()

.toUri();

 

HttpHeaders headers = new HttpHeaders();

Charset utf8 = Charset.forName("UTF-8");

MediaType mediaType = new MediaType("application", "json", utf8);

headers.setContentType(mediaType);

 

HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);

 

ParameterizedTypeReference<List<LocationResponse>> responseType = new ParameterizedTypeReference<List<LocationResponse>>() { };

ResponseEntity<List<LocationResponse>> responseEntity = restTemplate.exchange(targetUrl.toURL().toString(), HttpMethod.GET, entity, responseType);

List<LocationResponse> result = responseEntity.getBody();

 

logger.info(result);

}

}

 



출처: http://knight76.tistory.com/entry/Spring-RestTemplate-utf8-json-예제 [김용환 블로그(2004-2018)]

번호 제목 글쓴이 날짜 조회 수
공지 (확인전) [2021.03.12] Eclipse에서 Spring Boot로 JSP사용하기(Gradle) 황제낙엽 2023.12.23 0
공지 [작성중/인프런] 스프링부트 시큐리티 & JWT 강의 황제낙엽 2023.12.20 6
83 java.util.MissingResourceException: Can't find bundle for base name xxx, locale ko_KR 황제낙엽 2007.06.21 2362
» [Spring3.1.1][1] RestTemplate 한글 문제 황제낙엽 2018.08.08 683
81 Spring WebFlow Introduction (웹개발을 직관적으로) 황제낙엽 2006.12.09 555
80 Spring Security OAuth2구현 file 황제낙엽 2019.09.05 462
79 Spring Security OAuth 황제낙엽 2019.09.05 435
78 <spring:checkbox> tip! 황제낙엽 2007.10.01 378
77 [Spring3.1.1][3] RestTemplate 한글 문제 황제낙엽 2018.08.08 237
76 Spring MVC 가 아닌 환경에서 Spring Pojo Bean 사용하기 (Pure Java App 또는 Servlet App) 황제낙엽 2009.10.22 233
75 Cugain의 샘플프로젝트 jpetstore 분석기 - (5) applicationContext.xml 분석 황제낙엽 2007.04.21 232
74 스프링 개발팁 황제낙엽 2007.08.17 223
73 Spring Boot에서의 RESTful API 모듈 file 황제낙엽 2020.04.16 216
72 SimpleFormController 정리 황제낙엽 2007.09.19 206
71 Spring 과 Spring Boot의 차이 file 황제낙엽 2020.05.26 202
70 Spring의 Exception 황제낙엽 2007.09.17 194
69 Spring MVC 어플리케이션 개발 <11> 간단한 조회 구현 방안 비교 황제낙엽 2007.05.27 164
68 스프링 2와 JPA 시작하기 (한글) 황제낙엽 2007.08.27 142
67 [Spring3.1.1] ResponseBody 한글깨짐 해결법 황제낙엽 2018.08.08 140
66 웹 개발의 변화와 스프링 황제낙엽 2008.03.19 132
65 Spring AOP - Pointcut 황제낙엽 2007.10.02 129
64 유효성체크 (org.springframework.validation.Validator) 황제낙엽 2007.08.17 129