Java declaration of declarative hyperlink @Ref Using annotation

I tried to expand on the example given in chapter 6 (declarative hyperlink) of the Jersey 1.12 documentation, but it seems to hit the wall due to using the @Ref annotation.

My code is as follows:

@Path("/offerings/{offeringId}/widgets") @Produces(MediaType.APPLICATION_JSON) public class WidgetsResource { @GET @Path("/{widgetId}") public Response get(@PathParam("offeringId") String offeringId, @PathParam("widgetId") String widgetId) { Widgets widgets = new Widgets(); widgets.setOfferingId(Integer.valueOf(offeringId)); Widget widget = new Widget(); widget.setId(Integer.valueOf(widgetId)); widgets.setWidgets(Arrays.asList(widget)); return Response.status(200).entity(widgets).build(); } } public class Widgets { @Ref(resource = WidgetsResource.class, style=Style.ABSOLUTE) URI uri; @JsonIgnore private int offeringId; private Collection<Widget> widgets; public Collection<Widget> getWidgets() { return widgets; } public void setWidgets(Collection<Widget> widgets) { this.widgets = widgets; } public URI getUri() { return uri; } public int getOfferingId() { return offeringId; } public void setOfferingId(int id) { this.offeringId = id; } } public class Widget { @Ref(resource = WidgetsResource.class, style=Style.ABSOLUTE, bindings={ @Binding(name="offeringId", value="${entity.offeringId}")} ) URI uri; private int id; public URI getUri() { return uri; } public int getId() { return id; } public void setId(int id) { this.id = id; } } 

This works fine for the URL generated for the instance of the Widgets collection object:

 "uri": "http://localhost:65080/<app>/offerings/9999/widgets" 

However, I want to know how I can add the id of Widget instances in the collection to the URL for each widget. Thus, the generated URI will look something like this:

 "uri": "http://localhost:65080/<app>/offerings/9999/widgets/1234" 

I cannot find a way to use the Ref annotation to achieve this without starting to hardcode the entire path value in the Widget class, which I would like to avoid if possible.

Is there a standard way to achieve this?

+7
source share
1 answer

My reading of this documentation says that you can do something like this (untested!):

 @Ref(value="/offerings/{offeringId}/widgets/{widgetId}", style=ABSOLUTE) URI uri; 
+1
source

All Articles