HATEOAS paths are not valid when using the API gateway in a Spring Boot application

I have two spring boot applications, one of which acts as an API gateway (as discussed in the Spring Example ). The other, which is connected to the first, represents the profile service using spring-data-rest (spring-data-neo4j-rest).

The first application runs on port 8080 and uses zuul to route requests to the second as follows:

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

All this works fine, and requests to http: // localhost: 8080 / profile are served from a second application. The problem is that the HATEOAS links in the response are incorrect. The answer from the call to this second service is correct:

 { "_links": { "self": { "href": "http://localhost:8083/profiles{?page,size,sort}", "templated": true }, "search": { "href": "http://localhost:8083/profiles/search" } }, "_embedded": { "profiles": [ { "name": "Andrew Rutter", "_links": { "self": { "href": "http://localhost:8083/profiles/0" } } }, { "name": "Andrew Rutter", "_links": { "self": { "href": "http://localhost:8083/profiles/1" } } } ] }, "page": { "size": 20, "totalElements": 2, "totalPages": 1, "number": 0 } } 

But when it returns to my Gateway API, the links are rewritten to

 { "name": "Andrew Rutter", "_links": { "self": { "href": "http://localhost:8080/profiles/profiles/0" } } } 

What is the alias of the gateway path plus the actual Uri service base. I skip the zuul option to disable this behavior and just leave the hateoas uri in place with the host setup. Or is there a way for my service behind the gateway to connect to / rather to the default resource endpoint / profile (in this case), which would avoid adding an unwanted path to.

Thanks!

+8
source share
6 answers

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

The X-Forwarded-Host header has been added to redirected requests by default. To disable it, set zuul.addProxyHeaders = false. The default path prefix is ​​removed, and the backend request has the header "X-Forwarded-Prefix" ("/ myusers" in the above examples).

You can try the recommended fix that should set zuul.addProxyHeaders=false

+6
source

I had exactly the same problem. Change the configuration as follows:

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

This directs all requests going to the gateway matching "/ profiles / **" to your server on the back panel " http: // localhost: 8083 " and the prefix goes away (in your case, "/ profiles", since this corresponds to the route) .

+3
source

Zuul is redirected to / profile contextPath.

Try setting this as a configuration:

 zuul: routes: profiles: path: /profiles/** url: http://localhost:8083/ 
0
source

After some time with the same problem, finally I tried zuul.addProxyHeaders = true and it works! Links are no longer broken.

0
source

In the demo application that I used for SpringOne in my talk about Spring Data REST, I have the following configuration for handling URI rewriting, as well as for setting prefix headers correctly.

 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 fully at https://github.com/gregturn/spring-a-gram/blob/master/spring-a-gram-frontend/src/main/resources/application.yml#L23-L32

0
source

I have the same problem, my HATEOAS links are for a real service, not a gateway. Details here: Spring Boot Zuul hateoas REST answer has direct service links in the resource

0
source

Source: https://habr.com/ru/post/988351/


All Articles