Question

I want to link to a method that has the following signature:

public SomeResponse getSomeObjects(@RequestParam(value = "foo", defaultValue = "bar") Foo fooValue)

Now I want the link to look like this:

http://myhost/api/someobjects

I tried using methodOn from Spring HATEOAS's ControllerLinkBuilder as seen below:

discoverResponse.add(linkTo(methodOn(SomeController.class).getSomeObjects(null)).withRel("someobjects"))

But it doesn't lead to the desired link because a ?foo is added at its end. How can I achieve the above objective?

Was it helpful?

Solution

Since backward compatibility is such an issue for you, you could always manually construct your Link objects like so:

discoverResponse.add(new Link(baseUri() + "/someobjects", "someobjects"));

The other option would be to fork Spring HATEOAS on GitHub, build the project yourself, and change the way defaults are handled in ControllerLinkBuilder. I don't really know how you'd expect an out-of-context Link builder to be able to differentiate between whether it should advertise an optional parameter. In the HATEOAS world, if the parameter isn't included, the client doesn't know about it. So why even have the optional parameter?

OTHER TIPS

I know there are 7 years gone now, but I had a similar problem today which lead me here. In spring hateoas 1.1.0 the behavior is slightly different, instead it will generate URI-Templates by default for non-required @RequestParams:

http://myhost/api/someobjects{?foo}

If you don't want them in your link, you can just expand it

Map<String, Object> parameters = new HashMap<>();
parameters.put("foo", null);
link = link.expand(parameters);

It will result in the desired URL

http://myhost/api/someobjects
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top