spring cloud zuul filter返回中文乱码问题

  @Override
        public Object run() {
            RequestContext ctx = RequestContext.getCurrentContext();
            HttpServletRequest request = ctx.getRequest();
           
            Object accessToken = request.getHeader("token");
            if(accessToken == null) {
                ctx.setSendZuulResponse(false);
                ctx.setResponseStatusCode(401);
                try {
                    ctx.getResponse().getWriter().write("token is empty");
                }catch (Exception e){
                    e.printStackTrace();
                }

                return null;
            }
            
            return null;
        }

如果输出中文,前端会显示乱码
ctx.getResponse().getWriter().write("你好");

需要在前边加上这行代码:

ctx.getResponse().setContentType("text/html;charset=UTF-8");

你可能感兴趣的:(spring cloud zuul filter返回中文乱码问题)