<dependencies> <dependency> <groupId>org.springframework.hateoas</groupId> <artifactId>spring-hateoas</artifactId> <version>0.22.0.RELEASE</version> </dependency> </dependencies>
/** * */ package com.doj.hateoas.ws.accounts; import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo; import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn; import java.util.ArrayList; import java.util.List; import java.util.logging.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.hateoas.Resource; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; /** * @author Dinesh.Rajput * */ @RestController public class AccountController { protected Logger logger = Logger .getLogger(AccountController.class.getName()); @Autowired AccountRepository accountRepository; @RequestMapping("/accounts") public List<Resource<Account>> all() { logger.info("accounts all() invoked"); List<Account> accounts = accountRepository.getAllAccounts(); List<Resource<Account>> resources = new ArrayList<Resource<Account>>(); for (Account account : accounts) { resources.add(getAccountResource(account)); } logger.info("accounts all() found: " + accounts.size()); return resources; } @RequestMapping(value= "/accounts/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public Resource<Account> byId(@PathVariable("id") Long id) { logger.info("accounts byId() invoked: " + id); Account account = accountRepository.getAccount(id.toString()); Resource<Account> resource = new Resource<Account>(account); resource.add(linkTo(methodOn(AccountController.class).all()).withRel("accounts")); resource.add(linkTo(methodOn(AccountController.class).findAccountHolderById(account.getAccountId(), account.getAccountHolder().getUserId())).withRel("accountHolder")); logger.info("accounts byId() found: " + account); return resource; } @RequestMapping(value= "/accounts/{id}/{userId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public Resource<AccountHolder> findAccountHolderById(@PathVariable("id") Long id, @PathVariable("userId") Long userId) { logger.info("accounts findAccountHolderById() invoked: " + id); Account account = accountRepository.getAccount(id.toString()); AccountHolder accountHolder = account.getAccountHolder(); Resource<AccountHolder> resource = new Resource<AccountHolder>(accountHolder); resource.add(linkTo(methodOn(AccountController.class).byId(account.getAccountId())).withRel("account")); logger.info("accounts findAccountHolderById() found: " + account); return resource; } private Resource<Account> getAccountResource(Account account) { Resource<Account> resource = new Resource<Account>(account); resource.add(linkTo(methodOn(AccountController.class).byId(account.getAccountId())).withSelfRel()); resource.add(linkTo(methodOn(AccountController.class).findAccountHolderById(account.getAccountId(), account.getAccountHolder().getUserId())).withRel("accountHolder")); return resource; } }
[ { accountId: 1000, accountHolder: { userId: 5115, name: "Arnav", address: "Noida" }, amount: 1039.13, ifscCode: "AA992QA", links: [ { rel: "self", href: "http://localhost:1111/accounts/1000" }, { rel: "accountHolder", href: "http://localhost:1111/accounts/1000/5115" } ] }, { accountId: 2000, accountHolder: { userId: 2089, name: "Anamika", address: "Noida" }, amount: 1239.43, ifscCode: "AB966QJ", links: [ { rel: "self", href: "http://localhost:1111/accounts/2000" }, { rel: "accountHolder", href: "http://localhost:1111/accounts/2000/2089" } ] }, { accountId: 3000, accountHolder: { userId: 1286, name: "Dinesh", address: "Noida" }, amount: 3339.61, ifscCode: "AD912SA", links: [ { rel: "self", href: "http://localhost:1111/accounts/3000" }, { rel: "accountHolder", href: "http://localhost:1111/accounts/3000/1286" } ] } ]This will basically list all the accounts available at our repository.
{ accountId: 1000, accountHolder: { userId: 5115, name: "Arnav", address: "Noida" }, amount: 1039.13, ifscCode: "AA992QA", _links: { accounts: { href: "http://localhost:1111/accounts" }, accountHolder: { href: "http://localhost:1111/accounts/1000/5115" } } }It also provides links to any account holder of any account detail of our api telling it the URLs it can use to:
{ userId: 5115, name: "Arnav", address: "Noida", _links: { account: { href: "http://localhost:1111/accounts/1000" } } }This account holder detail api has link for associated account detail for this user
/** * */ package com.doj.hateoas.ws.accounts; import java.io.Serializable; import org.springframework.hateoas.ResourceSupport; /** * @author Dinesh.Rajput * */ public class Account extends ResourceSupport implements Serializable { /** * */ private static final long serialVersionUID = 1L; private Long accountId; private AccountHolder accountHolder; private Double amount; private String ifscCode; public Long getAccountId() { return accountId; } public void setAccountId(Long accountId) { this.accountId = accountId; } public AccountHolder getAccountHolder() { return accountHolder; } public void setAccountHolder(AccountHolder accountHolder) { this.accountHolder = accountHolder; } public Double getAmount() { return amount; } public void setAmount(Double amount) { this.amount = amount; } public String getIfscCode() { return ifscCode; } public void setIfscCode(String ifscCode) { this.ifscCode = ifscCode; } public Account(Long accountId, AccountHolder accountHolder, Double amount, String ifscCode) { super(); this.accountId = accountId; this.accountHolder = accountHolder; this.amount = amount; this.ifscCode = ifscCode; } @Override public String toString() { return "Account [accountId=" + accountId + ", accountHolder=" + accountHolder + ", amount=" + amount + ", ifscCode=" + ifscCode + "]"; } }
Labels: Spring HATEOAS