如何防止 spring boot gateway 转发到链中的最后一个元素
How to prevent springboot gateway to forward to last element in the chain
我正在使用 springboot 网关在我的微服务之间进行负载均衡请求
spring:
application:
name: gateway-service
cloud:
gateway:
routes:
- id: baeldung
uri: question.com/questions/444/ask
- id: myOtherRouting
uri: answer.com/answers/333/answer
我的问题是,如果我发送了错误的请求,例如 "question.com/questions/dasldkjasdas"
然后请求被转发到 answer.com/answers/333/answer
因为这是我配置中的最后一条链,我该如何防止这种情况,并发送 notfound 状态码作为响应。我用了
filters:
- SetStatus=NOT_FOUND
但这不是我认为最好的方法。
您没有为您的 route
定义指定任何 predicate
- 这意味着它们都匹配任何传入的请求。来自 Spring Cloud Gateway:
Clients make requests to Spring Cloud Gateway. If the Gateway Handler Mapping determines that a request matches a Route, it is sent to the Gateway Web Handler.
和
Route: Route the basic building block of the gateway. It is defined by an ID, a destination URI, a collection of predicates and a collection of filters. A route is matched if aggregate predicate is true.
由于您没有指定任何 predicate
- 您的路线与传入请求相匹配,最后一个获胜。
我正在使用 springboot 网关在我的微服务之间进行负载均衡请求
spring:
application:
name: gateway-service
cloud:
gateway:
routes:
- id: baeldung
uri: question.com/questions/444/ask
- id: myOtherRouting
uri: answer.com/answers/333/answer
我的问题是,如果我发送了错误的请求,例如 "question.com/questions/dasldkjasdas"
然后请求被转发到 answer.com/answers/333/answer
因为这是我配置中的最后一条链,我该如何防止这种情况,并发送 notfound 状态码作为响应。我用了
filters:
- SetStatus=NOT_FOUND
但这不是我认为最好的方法。
您没有为您的 route
定义指定任何 predicate
- 这意味着它们都匹配任何传入的请求。来自 Spring Cloud Gateway:
Clients make requests to Spring Cloud Gateway. If the Gateway Handler Mapping determines that a request matches a Route, it is sent to the Gateway Web Handler.
和
Route: Route the basic building block of the gateway. It is defined by an ID, a destination URI, a collection of predicates and a collection of filters. A route is matched if aggregate predicate is true.
由于您没有指定任何 predicate
- 您的路线与传入请求相匹配,最后一个获胜。