Java – special character encoding of errors using resteasy
I am using JBoss EAP 6.3 to develop a web application using resteasy rest framework. There is a bad coding problem with the special characters I pass in formparam in post resources, such as:
@POST @Path("/post") public Response createTask(@FormParam("param") String param) { LOGGER.info("HELLO POST XML. "+param); return Response.ok(param).build(); }
If I pass something like ABC è è è, I will get something like "ABC ÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃ195
What should I do?
thank you
Solution
Resteasy solution
Since resteasy uses servlets to interpret the request to you, it is best to use servlet filters 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); } }
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 the client request, you must 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
Set connector uriencoding:
<Connector port="8080" URIEncoding="UTF-8" />
Refer to JBoss seam documentation: 16.1 internationalizing your app
By changing the standalone. In JBoss as 7 and later XML (such as this answer (also answered in JBoss Developer Forum)) to complete this configuration
Server independent solution
Since the above solutions are dependent on JBoss, my answer will not be complete without providing a server independent solution
The most basic is to use the context parameter to indicate the character encoding selection of all forms in the application Set context parameters in WEB-INF / Web XML file
<context-param> <param-name>PARAMETER_ENCODING</param-name> <param-value>UTF-8</param-value> </context-param>
Your application can then read context parameters and set the request character encoding before reading any request parameters You can set the request encoding using java servlet or JSP syntax:
<% String paramEncoding = application.getInitParameter("PARAMETER_ENCODING"); request.setCharacterEncoding(paramEncoding); String name = request.getParameter("NAME"); %>
Refer to character conversions from browser to database
Database participation
You may still need to set the character encoding of the database, otherwise you may lose information, as shown in the following figure:
Refer to character conversions from browser to database
miscellaneous
For other information, see character encoding JSP - displayed wrong in JSP but not in URL. Tomcat is HttpServletRequest – SetCharacterEncoding seems to do nothing
You can also set the default encoding for the JVM
In resteasy version 2.3 7 fixed the error titled "text responses should default to charset UTF-8"