小白学习spring-cloud(十四):Spring Cloud Gateway过滤器精确控制异常返回(实战,控制http返回码和message字段)

63 阅读3分钟

第一种:抛出ResponseStatusException异常

  • 打开gateway-change-body工程的RequestBodyRewrite.java文件,改动如下图红框,如果请求body不含user-id参数就返回Mono.error,入参是ResponseStatusException异常,设置了返回码为400message为一段中文描述: image.png
package com.example.function;

import com.example.exception.MyGatewayException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.reactivestreams.Publisher;
import org.springframework.cloud.gateway.filter.factory.rewrite.RewriteFunction;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import java.util.Map;


@Slf4j
public class RequestBodyRewrite implements RewriteFunction<String, String> {

    private ObjectMapper objectMapper;

    public RequestBodyRewrite(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    /**
     * 根据用户ID获取用户名称的方法,可以按实际情况来内部实现,例如查库或缓存,或者远程调用
     * @param userId
     * @return
     */
    private  String mockUserName(int userId) {
        return "user-" + userId;
    }

    @Override
    public Publisher<String> apply(ServerWebExchange exchange, String body) {
        try {
            Map<String, Object> map = objectMapper.readValue(body, Map.class);

            if (!map.containsKey("user-id")) {
//                return Mono.error(new Exception("user-id参数不存在!"));

                // 抛出ResponseStatusException异常
                return Mono.error(new ResponseStatusException(HttpStatus.BAD_REQUEST, "请求参数必须包含user-id字段!"));
            }

            // 取得id
            int userId = (Integer)map.get("user-id");

            // 得到nanme后写入map
            map.put("user-name", mockUserName(userId));

            // 添加一个key/value
            map.put("gateway-request-tag", userId + "-" + System.currentTimeMillis());

            return Mono.just(objectMapper.writeValueAsString(map));
        } catch (Exception ex) {
            log.error("1. json process fail", ex);
            // json操作出现异常时的处理
            return Mono.error(new Exception("1. json process fail", ex));
        }
    }
}
  • 接下来运行nacosnacos-provider工程、gateway-change-body工程
  • postman发请求试试,请求和响应的详情如下图: ca2786471d741f1dd016eabd99b5277.png
  • 从上图可见,返回码为400,和我们设定的一样,但是message却为空,这是怎么回事呢?按照咱们的设定,这里应该显示请求参数必须包含user-id字段,看来咱们遇到一只拦路虎了

小小拦路虎

  • 咱们代码中,抛异常的时候设定message内容如下图红框所示,但运行的时候返回的是空字符串,这是怎么回事呢? image.png
  • 来看DefaultErrorWebExceptionHandler.isIncludeMessage方法,看下图红框中的那个errorProperties,您会不会恍然大悟:这不就是springboot配置中的error配置嘛!
  • 修改工程的配置文件,红框内是新增的配置: f1a4e3278fd56923d37177d526c3a17.png
server:
  #服务端口
  port: 10014
  # 异常情况时候的配置
  error:
    # 控制返回异常信息
    include-message: always
    # 控制返回异常类型
    include-exception: true
    # 控制返回精确的错误堆栈
    include-stacktrace: always
spring:
  application:
    name: gateway-change-body
  cloud:
    gateway:
      routes:
        - id: path_route_str
          uri: http://localhost:9001
          predicates:
            - Path=/nacos/test
  • 再用postman试试,如下图,这一次,statusmessageexceptiontrace齐聚一堂,完全符合预期: fadc6e6d8f350a4ef22eb0f32f42124.png

  • 看来第一种方法是可行的:返回ResponseStatusException类型的异常

第二种:自定义异常,带ResponseStatus注解

  • 首先新建一个异常类MyGatewayException.java,使用了ResponseStatus,在里面配置返回码和message内容,这次的返回码用的是403
package com.example.exception;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(code = HttpStatus.FORBIDDEN, reason = "user-id字段不能为空")
public class MyGatewayException extends Exception {
}
  • 修改RequestBodyRewrite,如下: image.png
  • 编码完成,重启应用,然后再发一次请求,如下图,返回码和message内容都符合预期: 620fba4346bbcc971d0447b9a459c5e.png
  • 至此,两种最简单的方式都完成验证,一般情况下已经满足要求:将错误信息准确传递给调用方

留有瑕疵

返回body的格式和字段都是固定的,如果项目中对返回body的内容有严格要求,例如只允许codemessagedata三个字段,其余字段一律不能返回,此时又该怎么办呢?