HATEOAS paths are invalid when using an API Gateway in a Spring Boot app

后端 未结 5 2061
时光说笑
时光说笑 2020-12-16 04:15

I have two spring boot applications where one of them is acting as an API Gateway (as discussed here Spring Example). The other which is wired into the first one is exposing

相关标签:
5条回答
  • 2020-12-16 04:17

    Zuul or Spring-Cloud adds the "X-Forwarded-Host" header to all the forwarded requests, which Spring-hateoas respects and modifies the links appropriately. To quote from Spring-Cloud docs:

    The X-Forwarded-Host header added to the forwarded requests by default. To turn it off set zuul.addProxyHeaders = false. The prefix path is stripped by default, and the request to the backend picks up a header "X-Forwarded-Prefix" ("/myusers" in the examples above).

    You can try the recommended fix, which is to set the zuul.addProxyHeaders=false

    0 讨论(0)
  • 2020-12-16 04:20

    I had exactly the same problem. Change your config as follows:

    zuul:
      routes:
        profiles:
          path: /profiles/**
          url: http://localhost:8083
          stripPrefix: false
    

    This routes all requests going to the gateway matching "/profiles/**" to your back end server "http://localhost:8083" and leaves the prefix (in your case "/profiles" since that's what matched the route).

    0 讨论(0)
  • 2020-12-16 04:34

    In the demo app I used for SpringOne in my talk about Spring Data REST, I have the following configuration to both handle URI rewrites as well as adjust prefix headers set properly.

    zuul:
      routes:
        api:
          path: /api/**
          serviceId: spring-a-gram-backend
          stripPrefix: false
        files:
          path: /files/**
          serviceId: spring-a-gram-mongodb-fileservice
          stripPrefix: false
    

    See it in full at https://github.com/gregturn/spring-a-gram/blob/master/spring-a-gram-frontend/src/main/resources/application.yml#L23-L32

    0 讨论(0)
  • 2020-12-16 04:37

    After struggling some time with the same problem, finally I've tried zuul.addProxyHeaders = true and it works! Links are not broken anymore.

    0 讨论(0)
  • 2020-12-16 04:40

    Zuul forwards to the /profiles contextPath.

    Try setting this as configuration:

    zuul:
      routes:
        profiles:
          path: /profiles/**
          url: http://localhost:8083/
    
    0 讨论(0)
提交回复
热议问题