Restful application with Spring MVC

This is quick guide example using Spring MVC to build Restful application. In this turtorial i will create simple application provide REST API to create,retrieve, update, delete people on Contact application.

Let’s get started

1. Things we need

We use following tool and technologies:

  1. Ecipse 4.3

  2. Maven 3

  3. Spring MVC 4.0.2

  4. JDK 7

2. Project structure

 

3. POM file

Spring dependency

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>

 

To Spring MVC support remember add jackson dependency

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.databind-version}</version>
</dependency>

4. Controller

@Controller

@RequestMapping("/contact")

public class PersonController {

@RequestMapping(value = "/{id}", method = RequestMethod.GET)

public @ResponseBody

Response get(@PathVariable(value = "id") Long id) {

Response response = new Response();

response.setSuccess(true);

response.setData(ContactDB.get(id));

return response;

}

@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)

public @ResponseBody Response delete(@PathVariable(value = "id") Long id) {

ContactDB.remove(id);

Response response = new Response();

response.setSuccess(true);

return response;

}

@RequestMapping(method = RequestMethod.POST)

public @ResponseBody Response update(@RequestBody Person person) {

ContactDB.update(person);

Response response = new Response();

response.setSuccess(true);

return response;

}

@RequestMapping(method = RequestMethod.PUT)

public @ResponseBody Response insert(@RequestBody Person person) {

ContactDB.add(person);

Response response = new Response();

response.setSuccess(true);

return response;

}

}

5. Demo

  • GET

    • Request: GET

    • Response:http://localhost:8080/spring-rest/contact/1

{

  "success": true,

  "data":    {

     "id": 1,

     "fname": "Thinh",

     "lname": "Pham",

     "mname": "Thai",

     "phone": "435435454",

     "address": "Home"

  }

POST

  • Request:

 {     "id":1,

     "fname": "Thinh3",

     "lname": "Pham",

     "mname": "Thai",

     "phone": "435435454",

     "address": "Home"

  }

 

  • Response:

{

  "success": true,

  "data": null

}
  • PUT

    • Request:

{

     "fname": "Thinh2",

     "lname": "Pham",

     "mname": "Thai",

     "phone": "435435454",

     "address": "Home"

  }

 

  • Response:

{

  "success": true,

  "data": null

}
{

  "success": true,

  "data": null

}

6. Source code: Download source code

 

One thought on “Restful application with Spring MVC”

Leave a Reply