RESTFul Web Service URI conventions with Spring MVC examples
The high level pattern for the RESTful URI is
- http(s)://myserver.com:8080/app-name/{version-no}/{domain}/{rest-reource-convetion}
- http(s)://myserver.com:8080/accounting-services/1.0/forecasting/accounts
- http(s)://myserver.com:8080/accounting-services/1.0/forecasting/account/123/transaction/567
What if you want to list a collection of transactions that are greater than a particular date?
- http(s)://myserver.com:8080/accounting-services/1.0/forecasting/account/123/transactions/search?txn-date=20120201
- http(s)://myserver.com:8080/accounting-services/1.0/forecasting/account/123/transaction
Finally, you can also control which method gets executed with the help of HTTP headers or host names in the URL. Let's see some Spring MVC examples in a controller class as to how it maps a request URI, headers, etc to execute the relevant method on the server side.
Here is the sample code with GET requests
@Controller
@RequestMapping("/forecasting")
public class CashForecastController
{
    @RequestMapping(
            value = "/accounts,
            method = RequestMethod.GET,
            produces = "application/json")
    @ResponseBody
    public AccountResult getAllAccounts(HttpServletResponse response) throws Exception
    {    
        //get the accounts via a service and a dao layers
    }  
 @RequestMapping(
            value = "/accounts.csv,
            method = RequestMethod.GET,
            produces = "text/csv")
    @ResponseBody
    public void getAllAccounts(HttpServletResponse response) throws Exception
    {    
        //produces a CSV download file
    }  
 
    @RequestMapping(
            value = "/account/{accountCd}",
            method = RequestMethod.GET,
            produces = "application/json")
    @ResponseBody
    public Account getAccount(
            @PathVariable(value = "accountCd") String accountCode, HttpServletResponse response) throws Exception
    {
        //get the accounts via a service and a dao layers
 }
 
 
 //accept only if there is a special header
 @RequestMapping(
            value = "/account/{accountCd}",
            method = RequestMethod.GET,
    headers =
            {
                "operation=special"
            }
            produces = "application/json")
    @ResponseBody
    public Account getAccountSpecial(
            @PathVariable(value = "accountCd") String accountCode, HttpServletResponse response) throws Exception
    {
        //get the accounts via a service and a dao layers
  //special handling based
 }
    @RequestMapping(
            value = "/account/{accountCd}/transaction/{transactionId}",
            method = RequestMethod.GET,
            produces = "application/json")
    @ResponseBody
    public Transaction getTransaction(
            @PathVariable(value = "accountCd") String accountCode, 
   @PathVariable(value = "transactionId") String txnId, 
   HttpServletResponse response) throws Exception
    {
        //get the accounts via a service and a dao layers
  //accountCode and txnId can be used here
 }
 
 @RequestMapping(
            value = "/account/{accountCd}/transactions/search",
            method = RequestMethod.GET,
            produces = "application/json")
    @ResponseBody
    public TransactionResult getTransactions(
            @PathVariable(value = "accountCd") String accountCode, 
   @RequestParam(value = "txn-date", required = true) @DateTimeFormat(pattern = "yyyyMMdd") Date txnDate,
   HttpServletResponse response) throws Exception
    {
        //get the accounts via a service and a dao layers
  //accountCode and txnDate can be used here
 }
} 
Here is the sample code with POST and PUT requests
@Controller
@RequestMapping("/forecasting")
public class CashForecastController
{
    @RequestMapping(
            value = "/account/transaction",
            method = RequestMethod.POST)
    public @ResponseBody Transaction addTransaction(@RequestBody Transaction txn, HttpServletResponse response)
            throws Exception
    {
       
        //logic to create a new Transaction records via service and dao layers
        
    }
 
 
  @RequestMapping(
            value = "/account/transaction",
            method = RequestMethod.PUT)
    public @ResponseBody Transaction modifyTransaction(@RequestBody Transaction txn, HttpServletResponse response)
            throws Exception
    {
       
        //logic to modify a Transaction record via service and dao layers
        
    }
} 
Do's and Don'ts
| 
 | 
Labels: RESTful, Spring MVC, Web services


0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home