我有一个下面的控制器(例如)方法:
@Autowired
EntityLinks entityLinks;
@GetMapping(value = "{userId}", params = "sortBy")
public Resource<User> getUser(@PathVariable Integer userId, @RequestParam String sortBy ){
Resource<User> userResource = new Resource<>(new User());
userResource.add(entityLinks.linkToSingleResource(getClass(), userId));
return userResource;
}GET http://localhost:8085/api/v1/test/11?sortBy返回给我:
{
"name": null,
"email": null,
"_links": {
"self": {
"href": "http://localhost:8085/api/v1/test/11"
}
}
}我关心的是,来自"href": "http://localhost:8085/api/v1/test/11"的任何客户端(或使用该应用程序接口的人)如何猜测该应用程序接口是否需要sortBy查询参数?sortBy需要什么值,例如id、name
有没有办法生成更有帮助的href?例如"href": "http://localhost:8085/api/v1/test/11{?sortBy=id,name}"。表示sortBy只能接受id和name
发布于 2019-09-09 15:45:13
您可以构建一个Link对象并将其添加到Resource。请尝试:
@GetMapping(value = "{userId}", params = "sortBy")
public Resource<User> getUser(@PathVariable Integer userId, @RequestParam String sortBy ){
Link selfLink = new Link(entityLinks.linkFor(User.class) + "/" + fooId + "{?sortBy=id,name}").withSelfRel();
Resource<User> resource = new Resource<>(new User());
resource.add(selfLink);
return userResource;
}https://stackoverflow.com/questions/57837254
复制相似问题