resthateoasspring-hateoas

HATEOAS link to method with optional requestparams


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?


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?