설치&설정 관련/Spring Framework
[TIP]Spring MVC 에서 ResponsBody로 String 을 전달시 한글 깨짐 현상 해결
lahuman
2017. 2. 23. 12:05
728x90
Spring MVC 에서 ResponsBody로 String 을 전달시 한글 깨짐 현상 해결
Controller에서 단순한 문자열(String)을 ResponseBody로 전달 할 경우, 깨지는 현상이 발생할수 있습니다. 코드는 다음과 같습니다.
@RequestMapping(value="/preview/{id}", method=RequestMethod.GET)
public @ResponseBody String getContent(@PathVariable("id") long id) {
return service.getContent(id);
}
한글이깨지는 원인은 브라우져에서 해당 요청에 대한 응답의 헤더 값을 보면 다음과 같이 표현 되어 있습니다.
Content-Type:application/json;charset=ISO-8859-1
위의 문제를 해결 하기 위해서는 다음과 같이 spring servlet xml 설정을 추가 해야 합니다.
<mvc:annotation-driven>
<mvc:message-converters>
<!-- @ResponseBody Content-Type:application/json;charset=UTF-8 -->
<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>
위와 같이 설정을 한 후 응답의 해더 값을 확인 하면 다음과 같습니다.
Content-Type:application/json;charset=UTF-8
이후 Content 값을 확인하면, 한글이 깨지지 않고 제대로 표출 되는 것을 확인 할 수 있습니다.
728x90