Declaratively adding links to the resource representation

We are now ready with all the necessary measures for using the Jersey declarative hyperlink feature. Let's get started on using the Jersey annotation in a recourse class to declaratively generate links in the resource representation generated at runtime.

Take a look at the following Department resource class. This class is almost the same as the one that we discussed a while ago in the Programmatically building entity body links using JAX-RS APIs section. One difference that you may find in the Department resource class used in this section is the presence of the @org.glassfish.jersey.linking.InjectLink annotation. This is used for generating hypermedia links in the resource representation and can be used on fields of the String or URI type. The following code snippet uses InjectLink for generating hypermedia links for accessing employee resources:

//Other imports are removed for brevity 
import org.glassfish.jersey.linking.Binding; 
import org.glassfish.jersey.linking.InjectLink; 
import org.glassfish.jersey.linking.InjectLink.Style; 
import javax.xml.bind.annotation.adapters.XmlAdapter;   
@XmlRootElement 
public class Department { 
 
    private Short departmentId; 
    private String departmentName; 
    private Integer managerId; 
    private Short locationId; 
 
   @InjectLink( 
            value = "{id}/employees", 
            style = Style.RELATIVE_PATH, 
            bindings = @Binding(name = "id",  
            value = "${instance.departmentId}"), 
            rel = "employees" 
    ) 
    @XmlJavaTypeAdapter(XmlAdapter.class) 
    @XmlElement(name = "link") 
    Link link; 
    //getters and setters for the properties are omitted for brevity 
} 

The entity providers offered by Jersey will take care of generating the appropriate representation for the preceding class when returned in response to a REST API call at runtime. For instance, when the preceding resource class is used with the public List<Department> findAllDeprtments() methods that we discussed in the Programmatically building entity body links using JAX-RS APIs section, the runtime will generate the appropriate hypermedia links as shown here (without you doing any extra coding, except using appropriate hyperlink annotations):

[{"link":{"href":"300/employees","rel":"employees"},"departmentId":300,"departmentName":"Administration","locationId":1700,"managerId":200}, ...] 

Let's take a look at the @InjectLink annotation to understand the usage better:

Here is a quick summary of the important elements that you can use in the @InjectLink annotation:

  • value: This element specifies a URI template that will be used to build the injected URI. The URI template may contain parameters referring to properties in the current object and Expression Language (EL) expressions. For instance, the ${instance.departmentId} EL expression refers to departmentId in the current object. The EL expression that you use here can refer to three types of implicit objects:
    • Instance: This represents the instance of the class that contains the annotated field.
    • Entity: This represents the entity class instance returned by the resource method. It is typically used in defining link headers.
    • Resource: This represents the resource class instance that returns the entity.
  • resource: This element specifies a resource class whose @Path URI template will be used to build the injected URI.
  • bindings: This element specifies the runtime binding for embedded URI template parameters. For instance, bindings = @Binding(name = "id", value = "${instance.departmentId}") indicates that the template parameter id is bound to the EL, ${instance.departmentId}, and value for id can be resolved from the EL that it is bound to.
  • condition: This element specifies a Boolean EL expression whose value determines whether a Link value can be injected into the resource.
  • style: This defines the style of the URI to inject, such as the relative path or the absolute path.
  • rel: This specifies the relationship with the target resource.
  • type: This specifies the media type for the referenced resource content, for example, application/json.
  • hreflang: This indicates the language for the referenced resource, for example, hreflang="en".
See the API doc to learn about all the elements available with @InjectLink: https://jersey.github.io/apidocs/latest/jersey/org/glassfish/jersey/linking/InjectLink.html.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.216.254