0

I am having below a controller(just for example) method:

@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 returns me:

{
    "name": null,
    "email": null,
    "_links": {
        "self": {
            "href": "http://localhost:8085/api/v1/test/11"
        }
    }
}

My concern is, from "href": "http://localhost:8085/api/v1/test/11" how could any client(or the person using the API) would guess if sortBy query param is required by the API? And what values sortBy demands e.g id,name

Is there any way to generate more helpfull href? e.g "href": "http://localhost:8085/api/v1/test/11{?sortBy=id,name}". Means sortBy can only accepts id and name

The Coder
  • 3,447
  • 7
  • 46
  • 81

1 Answers1

0

You can build a Link object and add it to Resource. Please try :

@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;
}
zpavel
  • 951
  • 5
  • 11