还记得在学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");即可,代码如下:
- public class CharacterEncodingFilter implements ContainerRequestFilter {
- @Context
- HttpServletRequest request;
- @Override
- public void filter(ContainerRequestContext requestContext) throws IOException {
- request.setCharacterEncoding("UTF-8");
- }
- }
如果你用dubbo+Rest的话,也可以用这种方式来解决。