06: Spring 4 MVC RESTful GET & POST methods Web Services Tutorial

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

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

POST method

http://localhost:8080/simpleWeb/entry/v1/forecasting/account

POST method

XML sent via HTTP Request

Use @RestController annotation instead of @Controller

By using “@RestController” you can eliminate the need for using “@ResponseBody” annotation and “ResponseEntity” as shown below.


Java Interview FAQs

800+ Java Interview Q&As

Top