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 1031
공지 [작성중/인프런] 스프링부트 시큐리티 & JWT 강의 황제낙엽 2023.12.20 1040
81 java.util.MissingResourceException: Can't find bundle for base name xxx, locale ko_KR 황제낙엽 2007.06.21 2750
80 Spring Boot PetClinic Sample Application 황제낙엽 2023.12.21 1061
» [Spring3.1.1][1] RestTemplate 한글 문제 황제낙엽 2018.08.08 975
78 Spring WebFlow Introduction (웹개발을 직관적으로) 황제낙엽 2006.12.09 763
77 Spring Security OAuth2구현 file 황제낙엽 2019.09.05 734
76 Spring Security OAuth 황제낙엽 2019.09.05 706
75 <spring:checkbox> tip! 황제낙엽 2007.10.01 620
74 Spring 과 Spring Boot의 차이 file 황제낙엽 2020.05.26 520
73 Spring MVC 가 아닌 환경에서 Spring Pojo Bean 사용하기 (Pure Java App 또는 Servlet App) 황제낙엽 2009.10.22 515
72 Spring Boot에서의 RESTful API 모듈 file 황제낙엽 2020.04.16 509
71 [Spring3.1.1][3] RestTemplate 한글 문제 황제낙엽 2018.08.08 505
70 SimpleFormController 정리 황제낙엽 2007.09.19 455
69 스프링 개발팁 황제낙엽 2007.08.17 449
68 Spring Framework 에서 사용하는 annotation 황제낙엽 2024.01.17 430
67 Cugain의 샘플프로젝트 jpetstore 분석기 - (5) applicationContext.xml 분석 황제낙엽 2007.04.21 422
66 [Spring3.1.1] ResponseBody 한글깨짐 해결법 황제낙엽 2018.08.08 405
65 Spring의 Exception 황제낙엽 2007.09.17 396
64 스프링 2와 JPA 시작하기 (한글) 황제낙엽 2007.08.27 379