'ResponseBody'에 해당되는 글 1건

  1. 2013.04.16 Spring @ResponseBody 로 리턴시 한글이 깨질때

Spring @ResponseBody 로 리턴시 한글이 깨질때



컨트롤러 메소드에서 

@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을 확인해 보고 고거에 맞는 값을 설정해 주면 된다.




prev 1 next