sitelink1 http://bcuts.tistory.com/32 
sitelink2  
sitelink3  
extra_vars6  

Spring에서 http 기반의 메시지 처리는 HttpMessageConverter를 이용한다.

 

RestTemplate 클래스의 postxxx 메서드를 이용해서 파라미터를 전송하면 FormHttpMessageConverter를 사용하게됨.

FormHttpMessageConverter는 넘겨주는 파라미터 타입에 따라서 HttpMessageConverter 종류를 다르게 사용함.

 

한글 깨지는 문제는 StringHttpMessageConverter의 기본 캐릭터셋이 UTF-8 아님.

 

해결책은 아래 설정 참조.

 

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

<property name="messageConverters">

<list>

<bean class="org.springframework.http.converter.FormHttpMessageConverter">

<property name="partConverters">

<list>

<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter">

</bean>

<bean class="org.springframework.http.converter.StringHttpMessageConverter">

<constructor-arg>

<bean class="java.nio.charset.Charset" factory-method="forName">

<constructor-arg value="UTF-8" />

</bean>

</constructor-arg>

<property name="writeAcceptCharset" value="false" />

</bean>

<bean class="org.springframework.http.converter.ResourceHttpMessageConverter">

</bean>

</list>

</property>

</bean>

<bean class="org.springframework.http.converter.StringHttpMessageConverter">

<constructor-arg>

<bean class="java.nio.charset.Charset" factory-method="forName">

<constructor-arg value="UTF-8" />

</bean>

</constructor-arg>

</bean>

</list>

</property>

</bean> 

 

 

 

 

 

 

 

 

아래부부은 <constructor-arg value="UTF-8" /> 이렇게 변경해도됨

 

 

<constructor-arg>

<bean class="java.nio.charset.Charset" factory-method="forName">

<constructor-arg value="UTF-8" />

</bean>

</constructor-arg> 

 

 

 

 

Java Code

 

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

FormHttpMessageConverter formHttpMessageConverter = new FormHttpMessageConverter();

formHttpMessageConverter.setCharset(Charset.forName("UTF-8"));

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

partConverters.add(new StringHttpMessageConverter(Charset.forName("UTF-8")));

partConverters.add(new ResourceHttpMessageConverter());

partConverters.add(new ByteArrayHttpMessageConverter());

formHttpMessageConverter.setPartConverters(partConverters);

messageConverters.add(formHttpMessageConverter);

 

messageConverters.add(new StringHttpMessageConverter(Charset.forName("UTF-8")));

 

RestTemplate restTemplate = new RestTemplate(messageConverters);

 

 



출처: http://bcuts.tistory.com/32 [Back to the BASIC]

번호 제목 글쓴이 날짜 조회 수
공지 (확인전) [2021.03.12] Eclipse에서 Spring Boot로 JSP사용하기(Gradle) 황제낙엽 2023.12.23 0
공지 [작성중/인프런] 스프링부트 시큐리티 & JWT 강의 황제낙엽 2023.12.20 6
63 [Spring3.1.1][4] RestTemplate 한글 문제 황제낙엽 2018.08.08 89
62 [Spring3.1.1][3] RestTemplate 한글 문제 황제낙엽 2018.08.08 237
» [Spring3.1.1][2] RestTemplate 한글 문제 황제낙엽 2018.08.08 113
60 [Spring3.1.1][1] RestTemplate 한글 문제 황제낙엽 2018.08.08 683
59 [Spring3.1.1] Eclipse 에 Spring Framework 환경 구축하기 file 황제낙엽 2018.08.08 90
58 웹 개발의 변화와 스프링 황제낙엽 2008.03.19 132
57 Spring MVC 가 아닌 환경에서 Spring Pojo Bean 사용하기 (Pure Java App 또는 Servlet App) 황제낙엽 2009.10.22 233
56 NamedParameterJdbcDaoSupport 몇가지 장점 황제낙엽 2007.11.27 101
55 프로젝트의 기본이 되는 Logging, Exception 처리 전략 황제낙엽 2007.01.30 85
54 Spring AOP - Pointcut 황제낙엽 2007.10.02 129
53 <spring:checkbox> tip! 황제낙엽 2007.10.01 378
52 SimpleFormController 정리 황제낙엽 2007.09.19 206
51 Spring의 Exception 황제낙엽 2007.09.17 194
50 스프링 2와 JPA 시작하기 (한글) 황제낙엽 2007.08.27 142
49 스프링 개발팁 황제낙엽 2007.08.17 223
48 유효성체크 (org.springframework.validation.Validator) 황제낙엽 2007.08.17 129
47 Spring 2.0의 XML확장기능 (3) 황제낙엽 2007.08.15 32
46 Spring 2.0의 XML확장기능 (2) 황제낙엽 2007.08.15 73
45 Spring 2.0의 XML확장기능 (1) 황제낙엽 2007.08.15 33
44 CSS와 XHTML을 사용한 효율적인 View 개발 전략 황제낙엽 2007.01.30 104