美文网首页
Spring Cloud Zuul header中文乱码

Spring Cloud Zuul header中文乱码

作者: 莫夏_b560 | 来源:发表于2020-11-10 16:17 被阅读0次
添加的时候编码:URLEncoder.encode("","UTF-8")
使用的时候解码:URLDecoder.decode("","UTF-8")

在Spring Cloud Zuul中添加filter的时候,要在header中加入含有中文属性的对象,在另外一个微服务中接受的时候,发现中文乱码,具体解决方案,是先用URLEncoder编码,然后微服务的接受的时候再解码

(1)Spring Cloud Zuul:

// obj这个对象中含有中文属性
RequestContext.getCurrentContext().addZuulRequestHeader("user", URLEncoder.encode(JSONObject.toJSONString(obj), "UTF-8"));

(2)微服务:

        String userStr = this.getRequest().getHeader("picaUser");
        User user = null;
        if (StringUtil.isNotNull(user)) {
            try {
                user = URLDecoder.decode(userStr, "UTF-8");//解码
            } catch (UnsupportedEncodingException var4) {
                var4.printStackTrace();
            }
       }

这样就能解决中文乱码的问题!

相关文章

网友评论

      本文标题:Spring Cloud Zuul header中文乱码

      本文链接:https://www.haomeiwen.com/subject/nhyjbktx.html