I have been trying to expand upon the example provided in Chapter 6 (Declarative Hyperlinking) of the Jersey 1.12 documentation but appear to have hit a wall with regard to the use of 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 an instance of the Widgets collection object:
"uri": "http://localhost:65080/<app>/offerings/9999/widgets"
However, I want to know how I can append the id of the Widget instances within the Collection to the URL for each Widget. So,the URI generated would be something like:
"uri": "http://localhost:65080/<app>/offerings/9999/widgets/1234"
I can't seem to find a way of using the Ref annotation to achieve this without starting to hardcode the whole path value within the Widget class, which I'd like to avoid if possible.
Is there a standard way of achieving this?