Bad special character encoding with RESTEasy

2019-01-19 08:17发布

问题:

I am developing a web application with jboss eap 6.3 which uses resteasy rest framework, I have got a bad encoding problem with special characters passed as FormParam in a POST resources, for example:

@POST
@Path("/post")
public Response createTask(@FormParam("param") String param) {
    LOGGER.info("HELLO POST XML. "+param);

    return Response.ok(param).build();

}

If I pass a thing like abc èèè i will get a stuff like "abc èà èà è", with jersey rest framework this issue don'exists.

What should i do?

Thanks

回答1:

RESTEasy solution

Since RESTEasy interprets the request for you using a servlet, your best bet is to use a servlet filter to set the request character encoding:

public class CharacterEncodingFilter implements javax.servlet.Filter {

  // ...

  @Override
  public void doFilter(ServletRequest request, ServletResponse response,
      FilterChain filterChain) throws IOException, ServletException {
    request.setCharacterEncoding("UTF-8");
    filterChain.doFilter(request, response);
  }
}  

Reference How to set charset for my web application?

JBoss solution

To ensure that the application server receives the request parameters in the correct encoding from client requests, you have to configure the connector. For JBoss AS (before version 7) change:

<jboss_install>/server/deploy/jbossweb.sar/server.xml

or in other JBoss AS versions:

<jboss_install>/server/(default)/deploy/jboss-web.deployer/server.xml

to set the connector URIEncoding:

<Connector port="8080" URIEncoding="UTF-8" />

Reference JBoss Seam documentation: 16.1 Internationalizing your app.

This configuration is done differently by changing standalone.xml in JBoss AS 7 and later, as in this answer (also answered in JBossDeveloper forum).

Server independent solution

Since the above is a JBoss dependent solution, my answer would not be complete without providing a server-independent solution.

The most basic is to use a context parameter indicating the character encoding choice for all forms in the application. Setting the context parameter is done in the WEB-INF/web.xml file.

<context-param>
  <param-name>PARAMETER_ENCODING</param-name>
  <param-value>UTF-8</param-value>
</context-param>

Then your application can read the context parameter and can set the request character encoding before reading any request parameters. You can set the request encoding in either a Java Servlet or in JSP syntax:

<%
  String paramEncoding = application.getInitParameter("PARAMETER_ENCODING");
  request.setCharacterEncoding(paramEncoding);
  String name = request.getParameter("NAME");
%>

Reference Character Conversions from Browser to Database.

Database involvement

You may still have to set the character encoding of your database, otherwise you can lose information as in this diagram:

Reference Character Conversions from Browser to Database.

Miscellaneous

Additional information at Character encoding JSP -displayed wrong in JSP but not in URL and for Tomcat at HttpServletRequest - setCharacterEncoding seems to do nothing.

You can also set the default encoding for the JVM.

A bug titled "Text responses should default to charset UTF-8" was fixed in RESTEasy version 2.3.7.



回答2:

I just declared in my project jboss-web.xml default encoding, and this solved the problem

<jboss-web>
   <default-encoding>UTF-8</default-encoding>
</jboss-web>