Search code examples
jerseyjax-rsdropwizardhateoasspring-hateoas

Is there a way to link JAX-RS resource to another resource like in Spring HATEOAS?


In Spring we've got @ExposesResourceFor annotation which can link our resource with other resources. Thanks to this our Value objects (representations) can know nothing of the actual resources.

Is there a way to do it in JAX-RS? I'm using Dropwizard with Jersey and Jackson and all I see is @InjectLinks annotation which I can use in a value object like this:

public class UserGroup {
    @JsonProperty
    public String name;

    @InjectLinks(GroupsResource.class)
    public URI myResource;

    public UserGroup(String name){
        this.name = name;
    }
}

But unfortunatelly my Value Objects should know nothing about Resources, so I'm asking can I do such linking on the level of resources - link in spring-hateoas in controllers, as mentioned above.


Solution

  • With @InjectLinks, you don't have to declare the links in your model class. You can create a "wrapper" representation class, as shown in declarative-linking from the Jersey examples (though this solution is not really on the resource class level as you wish).

    Another possible solution (rather than declarative linking) is to use the JAX-RS 2.0 Link class, and do the linking programmatically (with no ties to the Jersey implementation/annotations). You can either add the links to your response headers, as see here, or add Links to you model classes, as seen here (or use the wrapper class for this also, so as to not to invade your model classes)

    Some Resources