JAX-RS服务端接收中文乱码

还记得在学servlet的时候,处理中文乱码时:如果是GET提交则用byte[] nameByte = str.getBytes("ISO-8859-1"); str = new String(nameByte, "UTF-8");解决,如果是Post提交了则更容易,用request.setCharacterEncoding("UTF-8");来解决。

现在在处理JAX_RS的时候,可以在他提供的过滤器中写入request.setCharacterEncoding("UTF-8");即可,代码如下:

 

[html]  view plain copy
 
  1. public class CharacterEncodingFilter implements ContainerRequestFilter {  
  2.       
  3.     @Context  
  4.     HttpServletRequest request;  
  5.   
  6.     @Override  
  7.     public void filter(ContainerRequestContext requestContext) throws IOException {  
  8.         request.setCharacterEncoding("UTF-8");  
  9.     }  
  10.   
  11. }  


如果你用dubbo+Rest的话,也可以用这种方式来解决。

猜你喜欢

转载自zh-ka-163-com.iteye.com/blog/2265899