3

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?

4

2 回答 2

2

由于向后兼容性对您来说是个问题,您总是可以像这样手动构造您的 Link 对象:

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

另一种选择是在 GitHub 上 fork Spring HATEOAS,自己构建项目,并更改在ControllerLinkBuilder. 我真的不知道您如何期望上下文之外的链接构建器能够区分它是否应该宣传可选参数。在 HATEOAS 世界中,如果不包含参数,则客户端不知道它。那么为什么还要有可选参数呢?

于 2013-11-27T12:33:12.747 回答
1

我知道现在已经过去了 7 年,但今天我遇到了类似的问题,导致我来到这里。在 spring hatoas 1.1.0 中,行为略有不同,默认情况下它将为非必需的@RequestParams生成URI-Templates :

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

如果您不希望它们出现在您的链接中,您可以展开它

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

它将产生所需的 URL

http://myhost/api/someobjects
于 2021-10-25T11:33:22.717 回答