Search code examples
springcloudgateway

Spring cloud Gateway


I have anexo API with some endpoints, like:

 Localhsost:8080/api/clients  -> GET findall
 Localhsost:8080/api/clients/id -> GET findByID
 Localhsost:8080/api/clients -> POST insert a cliente
 Localhsost:8080/api/clients/id DELETE deleteByID

How do I use Spring Cloud Gateway with those endpoints?


Solution

  • If you're having trouble seeing where to start you could try following property-based the example from the dzone article 'Spring Cloud Gateway - Configuring a Simple Route'. You could configure just one of your services to begin with. That example suggests creating a spring cloud gateway project from the spring initializr by selecting the 'gateway' dependency and adding a route to the application.yaml:

    spring:
      cloud:
        gateway:
          routes: 
            - predicates:
                - Path=/props/**
              filters:
                - StripPrefix=1
              uri: "http://httpbin.org"
    

    So you could replace httpbin.org with localhost:8080 and replace /props/** with your path - /api/clients/**. You could test that by making an http get call and then try adding in a second service afterwards. In your case I suspect you want to remove the filter to strip the prefix as it sounds like your service is exposing an /api/clients endpoint so you'd presumably want to preserve that whole path. That's something you'd need to check.