강좌/Spring @MVC

@RequestBody 한글 깨질 때

여름나라겨울이야기 2013. 5. 13. 23:43
728x90

* the default charset of @ResponseBody is iso-8859-1, how to change to utf8.

출처: http://linuxism.tistory.com/720


@RequestMapping(value = "/path", produces="text/plain;charset=UTF-8")

public @ResponseBody String handlePath() {

    .....

}


produces="text/plain;charset=UTF-8"를 사용하여 응답 페이지에 대한 UTF-8 인코딩이 가능하여 한글 깨짐을 방지 할 수 있음.



GET 방식 인자 한글 처리(tomcat server.xml)


<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" useBodyEncodingForURI="true" URIEncoding="utf-8"/>


<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" useBodyEncodingForURI="true" URIEncoding="utf-8"/>



POST 방식 인자 한글 처리(web.xml)
- Filter 추가


<!-- Start: 한글문제 처리 -->
<filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>utf-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Finish: 한글문제 처리 -->


반응형