Spring HATEOAS provides the handy ControllerLinkBuilder
to create links to controller methods, which will be added as hrefs in the JSON/XML returned to a client. For instance:
resource.add(linkTo(methodOn(FooController.class)
.findFoo(entity.getClient().getId()))
.withRel("show"));
... might generate JSON a bit like:
{
"name":"foo",
"links":[
{"rel":"show","href":"http://111.11.11.111:28080/foos/1"}
]
}
However...
I tend to access my services through a reverse proxy. Which I guess most people probably would. This lets me have multiple services running on different ports, but lets me access them all through the same base URL. Unfortunately, accessing through a proxy means that the URL being generated by Spring HATEOAS is not a URL which is valid for accessing the resource.
Now I could just hard-code the links, but that's rather fragile. Having the ControllerLinkBuilder
generate URLs based on my controller @RequestMapping
configuration is valuable to me, as it avoids the risk of my links getting out of sync with reality.
So I was wondering whether there's a property somewhere that I could use to force the host and port values. I'm using Spring Boot, so ideally a property that I could add to the application.properties
file in each environment.
Note:
As this issue seems to be caused by a bug in Spring, I should probably point out that I'm using Spring Boot 1.0.2.RELEASE.
ControllerLinkBuilder
, it looks like it ought to be using theX-Forwarded-Host
header to generate the link. I'll do a bit more testing, but it may well be that my links are incorrect because the header is not being set correctly by my reverse proxy. – BrittainX-Forwarded-Host
header being set with no port. The host portion is being set correctly, so I'm trying to debug theControllerLinkBuilder
to establish why it seems to be appending theserver.port
value instead of leaving it off. – BrittainServletUriComponentsBuilder
which has also been copied into the code forControllerLinkBuilder
. I'll post an answer with the details. – Brittain