0

I have a search REST service implemented using Spring MVC that returns a response in JSON. The response contains hrefs (self, etc) that contain the search term. A Chinese search term is not displaying correctly in the hrefs, it is displayed like this:

%E5%A4%AA%E9%99%BD%E7%B3%BB%E4%B8%AD

I tried using URLEndoer.encode(myString, "UTF-8") but that didn't work.

EDIT

I found the root cause, it was a call to New URI(myparams).toAsciiString() that was causing the issue.

user86834
  • 5,357
  • 10
  • 34
  • 47
  • May this answer help you : http://stackoverflow.com/questions/5408340/spring-mvc-can-not-decode-chinese-characters – Jwalin Shah Feb 24 '14 at 11:04

3 Answers3

1

You need to make use of CharacterEncodingFilter class. CharacterEncodingFilter filter needs to be declared in web.xml in following manner.

<filter>  
    <filter-name>encodingFilter</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>encodingFilter</filter-name>  
    <url-pattern>/*</url-pattern>  
</filter-mapping> 
Dark Knight
  • 8,218
  • 4
  • 39
  • 58
0

What's the content-type header set to on the response? You can check by looking at the HTTP response using the developer tools in your browser.

It should be: application/json; charset=UTF-8

lance-java
  • 25,497
  • 4
  • 59
  • 101
0

While sending the response you can set the character encoding in your search method

this will make sure the returned response is in UTF-8 and the chinese character will get displayed @RequestMapping(value = "", method = RequestMethod.GET, produces = "application/json;charset=UTF-8") ResponseEntity<String> search(HttpServletRequest request, HttpServletResponse response); Edit: I tried with the filters in web.xml but it did not work.