Spring @ResponseBody 로 리턴시 한글이 깨질때
Spring 2013. 4. 16. 17:09
컨트롤러 메소드에서
@RequestMapping("/test") @ResponseBody public String test(){ return "mainBean : " + mainBean; }
요런식으로 한글이 섞인 문자열을 @ResponseBody로 리턴할때 브라우져에서 한글이 깨졌다.
요것을 해결할려면~
스프링설정 파일에 StringHttpMessageConverter에 대한 설정을 살포시 추가해주면 된다.
(싹 복사하지 말고 만약 기존에 설정이 되 있었으면 고거에다 쪼매 추가해 주면 된다.)
※ 스프링 3.0이랑 스프링 3.1 이상버전에서는 설정이 쪼매 틀리다.
스프링 3.0
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> </list> </property> </bean>
스프링 3.1에서 위와 같은 설정을 해보니
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter 요 클래스가 Deprecated 됐다면서 쪽바로 안됐다.
스프링 3.1이상에서는 요런식으로 mvc:annotation-driven 태그 안에 mvc:message-converters 태그로 설정을 해주면 된다.
스프링 3.1 이상
<mvc:annotation-driven> <mvc:message-converters> <!-- @ResponseBody로 String 처리할때 한글처리 --> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>
죠렇게 설정을 하고 나서 다시 해 보면 쨘~ 해결이 된다.
※ 죠기에 설정하는 text/html 값은 브라우져 관리자 도구를 열어 리턴되는 media type을 확인해 보고 고거에 맞는 값을 설정해 주면 된다.
'Spring' 카테고리의 다른 글
Spring profile 기능으로 운영/개발 환경 구성하기 (0) | 2013.04.17 |
---|---|
<util:properties/> 와 Spring EL 로 값 가져오기 (0) | 2013.04.12 |
Spring 개발시 개발, 운영환경 프로퍼티 파일 관리하기 (1) | 2011.10.11 |
bean property 간단하게 설정하기 : http://www.springframework.org/schema/p 활용 (1) | 2011.08.22 |
ContentNegotiatingViewResolver 활용 : 하나의 RequestMapping 으로 JSP, JSON, JSONP 처리하기 (0) | 2011.08.19 |