3

I have a simple RestController which returns a unicode string:

@RestController
@RequestMapping(path = "/")
public class FooController {
    @RequestMapping(method = RequestMethod.GET)
    public String test() throws Exception {
        return "ỗ ẵ ẫ ậ ự";
    }
}

But when run, it displays just "? ? ? ? ?", How can I resolve this problem, I already had this configuration in web.xml:

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

And when I test with simple HttpServlet, it can return above unicode string.

yelliver
  • 5,648
  • 5
  • 34
  • 65

1 Answers1

1

In case others encounter the same...
Add Produces annotation with charset=utf-8
Then use Response object like:

@RestController
@RequestMapping(path = "/")
@Produces(MediaType.TEXT_PLAIN + ";charset=utf-8")
public class FooController {
    @RequestMapping(method = RequestMethod.GET)
    public Response test() throws Exception {
          return Response.status(HttpStatus.OK).entity("ỗ ẵ ẫ ậ ự").build();
    }
}
JavaSheriff
  • 7,074
  • 20
  • 89
  • 159