본문 바로가기
jQuery

Spring Ajax - Json 데이터 한글깨짐 현상

by 나는 코딩왕 2021. 2. 2.

Ajax - Json 데이터를 받았을때 영어나 숫자는 나오지만 한글이 깨져 '?' 로 나오는 오류 없애버리기

 

 

json 데이터를 콘솔에 찍었을때 다음과 같이 한글이 깨져버렸다

 

여러 방법을 찾아보았을때 3가지 방법중에 저는 마지막 3번째 방법으로 오류를 없애버릴 수 있었다 ㅎㅎ

 

 

해결방안 1 - Java 단에 produces = "application/text; charset=UTF-8" 추가

@RequestMapping(value="/wordcloudtest.do", method = RequestMethod.GET, produces = "application/text; charset=UTF-8" )
@ResponseBody
public String wordcloud(HttpServletRequest request,HttpServletResponse response,WordcloudDTO WordcloudDTO) throws Exception{
		
		
		List<WordcloudDTO> list = dao.list(WordcloudDTO);
		
		String json = null;
		try {
			json = new ObjectMapper().writeValueAsString(list);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
			
		
		
		return json;
	
}

 

 

 

해결방안 2 - jsp 화면단에 contentType: "application/json; charset:UTF-8" 추가  

$.ajax({
		type: "GET",
	    url: "wordcloudtest.do",
	    dataType: "json",
	    contentType: "application/json; charset:UTF-8", 
	    async: false
		}).responseText;

 

 

해결방안 3 bean 설정파일에 @ResponseBody 어노테이션의 설정에 인코딩을 UTF-8로 바꾸는 코드 작성

	<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>

 

 

세번째 방법 후 결과입니다~~

 

 

Json 데이터를 받았을때 영어나 숫자는 나오지만 한글이 깨져 '?' 로 나오는 오류 없애버리기 끝~

댓글