RESTEasy 的特殊字符编码错误 [英] Bad special character encoding with RESTEasy

查看:46
本文介绍了RESTEasy 的特殊字符编码错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 jboss eap 6.3 开发一个 web 应用程序,它使用 resteasy rest 框架,我遇到了一个糟糕的编码问题,在 POST 资源中作为 FormParam 传递了特殊字符,例如:

@POST@Path("/post")公共响应 createTask(@FormParam("param") String param) {LOGGER.info("HELLO POST XML."+param);返回 Response.ok(param).build();}

如果我通过像 abc èèè 这样的东西,我会得到像abc èà èà ¨"这样的东西,而在球衣休息框架中,这个问题不存在.

我该怎么办?

谢谢

解决方案

RESTEasy 解决方案

由于 RESTEasy 使用 servlet 为您解释请求,因此最好的办法是使用 servlet 过滤器来设置请求字符编码:

public class CharacterEncodingFilter 实现 javax.servlet.Filter {//...@覆盖public void doFilter(ServletRequest 请求,ServletResponse 响应,FilterChain filterChain) 抛出 IOException, ServletException {request.setCharacterEncoding("UTF-8");filterChain.doFilter(请求,响应);}}

参考 .

杂项

其他信息位于 字符编码 JSP - 在 JSP 中显示错误但在 URL 中没有显示HttpServletRequest - setCharacterEncoding 似乎什么都不做.

您还可以设置 JVM 的默认编码.

一个名为文本响应应默认为字符集 UTF-8"的错误是RESTEasy 版本 2.3.7 中已修复.

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

解决方案

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.

这篇关于RESTEasy 的特殊字符编码错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆