This tutorial extends Spring 4 MVC RESTful Web Service Beginner Tutorial step by step.
Controller that supports GET with path variable & POST methods
Step 1: The “AccountController.java” that has two additional methods to support requests like
GET: http://localhost:8080/simpleWeb/entry/v1/forecasting/account/123
POST: http://localhost:8080/simpleWeb/entry/v1/forecasting/account
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | package com.mytutorial.controller; import java.math.BigDecimal; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import com.mytutorial.model.Account; @Controller @RequestMapping("/v1/forecasting") public class AccountController { @RequestMapping(value = "/accounts", method = RequestMethod.GET, produces={MediaType.APPLICATION_JSON_VALUE}) public @ResponseBody Account[] getAccounts() { Account[] accounts = new Account[] { new Account("123", "John R", BigDecimal.valueOf(235.00)), new Account("345", "Peter J", BigDecimal.valueOf(2505.60)) }; return accounts; } @RequestMapping(value = "/account/{id}", method = RequestMethod.GET, produces={MediaType.APPLICATION_XML_VALUE}) public @ResponseBody Account getAccount(@PathVariable("id") String id) { //TODO: go to database and get the Account by id Account account = new Account(id, "John R", BigDecimal.valueOf(235.00)); return account; } @RequestMapping(value = "/account", method = RequestMethod.POST) public ResponseEntity<Account> saveAccount(@RequestBody Account account) { System.out.println("Creating:" + account); //TODO: Save to database return new ResponseEntity<Account>(account, HttpStatus.OK); } } |
Step 2: Package as simpleWeb.war and deploy to Tomcat’s “webapps” folder.
Step 3: Start the Tomcat server.
GET method with account id as a path variable
http://localhost:8080/simpleWeb/entry/v1/forecasting/account/123
1 2 3 4 5 6 | <Account> <id>123</id> <name>John R</name> <balance>235.0</balance> </Account> |
POST method
http://localhost:8080/simpleWeb/entry/v1/forecasting/account
1 2 3 4 5 6 | <Account> <id>123</id> <name>John R</name> <balance>235.0</balance> </Account> |
Use @RestController annotation instead of @Controller
By using “@RestController” you can eliminate the need for using “@ResponseBody” annotation and “ResponseEntity” as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | package com.mytutorial.controller; import java.math.BigDecimal; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.mytutorial.model.Account; @RestController @RequestMapping("/v1/forecasting") public class AccountController { @RequestMapping(value = "/accounts", method = RequestMethod.GET, produces={MediaType.APPLICATION_JSON_VALUE}) public Account[] getAccounts() { Account[] accounts = new Account[] { new Account("123", "John R", BigDecimal.valueOf(235.00)), new Account("345", "Peter J", BigDecimal.valueOf(2505.60)) }; return accounts; } @RequestMapping(value = "/account/{id}", method = RequestMethod.GET, produces={MediaType.APPLICATION_XML_VALUE}) public Account getAccount(@PathVariable("id") String id) { //TODO: go to database and get the Account by id Account account = new Account(id, "John R", BigDecimal.valueOf(235.00)); return account; } @RequestMapping(value = "/account", method = RequestMethod.POST) public Account saveAccount(@RequestBody Account account) { System.out.println("Creating:" + account); //TODO: Save to database return account; } } |