Tuesday, 21 April 2015

Declarative Hyperlinking using @InjectLink - Part 2

In my previous post, I talked about the set-up required to use declarative hyperlinking feature provided by Jersey 2.x. This post is a continuation to that post. In this post, we will create links to individual accounts based on the balance amount available in the account.
Let us first update the service to include a GET method, which returns information for a particular account. We also need to add a method that performs POST operation to update account information. For this, go to and add the below code:
    public Accounts accountsInfoById(@PathParam("id") int id) {
        for (int i = 0; i < accList.size(); i++) {
            if (accList.get(i).getAccount_number() == id) {
                return accList.get(i);
        return null;

    public AccountWrapper updateAccountById(@PathParam("id") int id, Accounts account) {
        List<Accounts> tempList = new ArrayList<Accounts>();
        Accounts ac = accountsInfoById(id);
        if (ac != null) {
            return new AccountWrapper(tempList);
        } else {
            return null;
Next, go to class and add @InjectLink annotations on the view & update fields.

    @InjectLink(resource = AccountService.class, method="accountsInfoById",  bindings ={@Binding(name = "id", value = "${instance.account_number}")},style =  Style.ABSOLUTE)
    private String view;
    @InjectLink(resource = AccountService.class, method="updateAccountById",  bindings ={@Binding(name = "id", value = "${instance.account_number}")}, condition = "${instance.balance >= 0}", style =  Style.ABSOLUTE)
    private String update;

Here, we have specified the InjectLink annotation with a resource attribute. In the resource attribute, we are providing the name of the class whose @Path annotation needs to be used to create the URI. The method parameter further refines the URI by providing the name of the method to look within the class. The @Path annotation specified on this method will be appended to the previously created URI. Using bindings attribute, the path parameter values are being specified. Since, in our case, the path parameter maps to the account number, we have specified the value to be account_number. At runtime, the current value of account number will be injected into this.

Run the service.

Below is the navigation to the update operation for account 111 by following the update link.

In this post, I have shown field level injection. It is also possible to perform method level injection. I will probably go through that in another post.

No comments:

Post a Comment