Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
169 views
in Technique[技术] by (71.8m points)

java - Bad special character encoding with RESTEasy

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

Database configured for ASCII

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...