Search code examples
javaspring-bootspring-data-jpamicroservicesapi-gateway

Unable to locate routes in spring Gateway


have created a very very basic Spring Cloud Gateway from https://start.spring.io/ with the following dependencies

dependencies {
  implementation 'org.springframework.boot:spring-boot-starter-webflux'
  implementation 'org.springframework.cloud:spring-cloud-starter-gateway'
  testImplementation 'org.springframework.boot:spring-boot-starter-test'
  testImplementation 'io.projectreactor:reactor-test'
}

dependencyManagement {
  imports {
    mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
  }
}

tasks.named('test') {
  useJUnitPlatform()
}

and my application.yaml is below

server:
  port: 9000
spring:
  cloud:
    gateway:
      routes:
        - id: primary-gateway-users
          uri: http://httpbin.org/
          predicates:
            - Path=uuid

have changes nothing in any of the code that is downloaded from https://start.spring.io/ anything i am missing here any configuration should i add other than this. and it's not working to me.i am getting the below output

enter image description here


Solution

  • The path predicate used to redirect to your service is a Spring PathMatcher patterns based on the good old AntPathMatcher

    When you do a request on your gateway the gateway will see the incoming request as /uuid.

    Your PatchMatcher however is uuid and that will not match.

    PathMatcher springMatcher = new AntPathMatcher();
    boolean springMatch = springMatcher.match("uuid", "/uuid");
    // will return false
    

    If you update your route like this

    spring:
      cloud:
        gateway:
          routes:
            - id: primary-gateway-users
              uri: http://httpbin.org/
              predicates:
                - Path=/uuid
    

    your gateway will properly forward the request to http://httpbin.org/