SpringCloundGateWay全局过滤器返回自定义json中文乱码

我在我的网关(GateWay)中尝试添加一个自定义的过滤器,用来检测用户是否携带Token,如果未携带Token那么就返回我自己定义的统一返回类,但是在此过程中,我却发现,当我向其返回中文提示信息时,其中文被返回为乱码,如下。

公共返回类

@Data
@NoArgsConstructor
@AllArgsConstructor
public class CommonResult<T> {
    public Integer code;
    public String msg;
    public T data;

    public static CommonResult success(){
        CommonResult commonResult = new CommonResult<>();
        commonResult.setCode(ResponseCode.SUCCESS.getCode());
        commonResult.setMsg(ResponseCode.SUCCESS.getMsg());
        return commonResult;
    }

    public static <T>CommonResult<T> success(T data){
        CommonResult commonResult = new CommonResult<>();
        commonResult.setCode(ResponseCode.SUCCESS.getCode());
        commonResult.setMsg(ResponseCode.SUCCESS.getMsg());
        commonResult.setData(data);
        return commonResult;
    }
    public static CommonResult fail(){
        CommonResult commonResult = new CommonResult<>();
        commonResult.setCode(ResponseCode.FAIL.getCode());
        commonResult.setMsg(ResponseCode.FAIL.getMsg());
        return commonResult;
    }

    public static <T>CommonResult<T> fail(T data){
        CommonResult commonResult = new CommonResult<>();
        commonResult.setCode(ResponseCode.FAIL.getCode());
        commonResult.setMsg(ResponseCode.FAIL.getMsg());
        commonResult.setData(data);
        return commonResult;
    }
}

GateWay全局过滤器

@Component
public class TokenFilter implements GlobalFilter{

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain){
        ServerHttpResponse response = exchange.getResponse();
        List<String> token = exchange.getRequest().getHeaders().get("token");
        if (Objects.isNull(token)){
            HashMap<String, Object> data = new HashMap<>();
            data.put("tip","未登录");
            try {
                return response
                        .writeWith(Flux.just(exchange.getResponse().bufferFactory()
                                .wrap(new ObjectMapper().writeValueAsString(CommonResult.fail(data)).getBytes(StandardCharsets.UTF_8))));
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
        }
        return chain.filter(exchange);
    }
}

经过一系列的检查,我发现向前台返回时需要进行设置UTF-8编码,可能在别的地方你会看到这样的例子。

response.setContentType("application/json;charset=utf-8");

当时你会发现在这里这种方式是行不通的,因为这里就没有这种方法,故你需要换一种方法进行添加。

response.getHeaders().add("Content-Type","application/json;charset=UTF-8");

所以对全局过滤器进行修改。

@Component
public class TokenFilter implements GlobalFilter{

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain){
        ServerHttpResponse response = exchange.getResponse();
        // 添加此代码
        response.getHeaders().add("Content-Type","application/json;charset=UTF-8");
        
        List<String> token = exchange.getRequest().getHeaders().get("token");
        if (Objects.isNull(token)){
            HashMap<String, Object> data = new HashMap<>();
            data.put("tip","未登录");
            try {
                return response
                        .writeWith(Flux.just(exchange.getResponse().bufferFactory()
                                .wrap(new ObjectMapper().writeValueAsString(CommonResult.fail(data)).getBytes(StandardCharsets.UTF_8))));
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
        }
        return chain.filter(exchange);
    }
}

修改后运行

至此,该问题解决

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值