spring-cloud-gateway路径路由谓词不接受”/”。

huangapple go评论93阅读模式
英文:

spring-cloud-gateway path route predicate don't accept "/"

问题

我有一个自定义的网关过滤器,它使用了在application.yml中定义的路由。当我想要将路由谓词从***/test/myTest更改为/public***时,请求返回404错误,似乎"/"没有被识别,因为我尝试将/test-myTest更改为/public,它可以正常工作。

请问如何使用格式为/name/name/**的谓词?以下是我的application.yml配置:

myTest-gateway:
  default-uri: https://app
  apps:
    /test/myTest: mymicroservice-name
  spring:
    cloud:
      gateway:
        enabled: false
        x-forwarded:
          proto-enabled: false 
        routes:
          - id: appsPrefixPathPublic
            uri: ${myTest-gateway.default-uri}
            predicates:
              - AppPath=/test/myTest
            filters:
              - StripPrefix=1
              - PrefixPath=/public

这是我收到的错误信息:

> 无内容
> 
> < 404 NOT_FOUND Not Found < Content-Type: [application/json] <
> Content-Length: [147]
> 
> {"timestamp":"2020-08-26T15:44:12.023+0000","path":"/test/myTest/whatever","status":404,"error":"Not
> Found","message":null,"requestId":"e7baeeeb-6"}
> 
> java.lang.AssertionError: Status expected:<200 OK> but was:<404
> NOT_FOUND>
英文:

I have a custom gateway filter that use the route written in application.yml,
When I want to change the route predicate from /test/myTest to /public the request return 404 it seemes like the "/" is not reconized because I tried to change /test-myTest to /public and it's work.

Any idea please how can I use a predicate with this format /name/name/**

this is my application.yml :

myTest-gateway:
default-uri: https://app
apps:
  /test/myTest: mymicroservice-name
spring:
  cloud:
   gateway:
     enabled: false
     x-forwarded:
      proto-enabled: false 
     routes:
      - id: appsPrefixPathPublic
         uri: ${myTest-gateway.default-uri}
         predicates:
           - AppPath=/test/myTest
         filters:
          - StripPrefix=1
          - PrefixPath=/public

this is the error that I got it :

> No content
>
> < 404 NOT_FOUND Not Found < Content-Type: [application/json] <
> Content-Length: [147]
>
> {"timestamp":"2020-08-26T15:44:12.023+0000","path":"/test/myTest/whatever","status":404,"error":"Not
> Found","message":null,"requestId":"e7baeeeb-6"}
>
> java.lang.AssertionError: Status expected:<200 OK> but was:<404
> NOT_FOUND>

答案1

得分: 2

文档

 spring:
   cloud:
     gateway:
       routes:
       - id:nameRoot
         uri:https://nameservice
         predicates:
         - Path=/name/**
         filters:
         - StripPrefix=2

当通过网关对/name/blue/red发出请求时,发送到nameservice的请求看起来像nameservice/red

您应将1更改为2。

英文:

From the documentation :

spring:
  cloud:
    gateway:
      routes:
      - id: nameRoot
        uri: https://nameservice
        predicates:
        - Path=/name/**
        filters:
        - StripPrefix=2

When a request is made through the gateway to /name/blue/red, the request made to nameservice looks like nameservice/red.

You should change 1 to 2.

huangapple
  • 本文由 发表于 2020年8月27日 00:34:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63601962.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定