Spring MVC 4 REST Example

Spring MVC 4 REST Example explains about creating a simple Spring MVC REST service using eclipse.

In this example we are using Jackson JSON API for returning JSON response according to the request

Now a days more and more deployment is going based on restful services compare to WSDL Webservices, due to the weights towards the simplicity of configuration

You can see the below example, which is demonstrating How to create a Spring Restful service using MVC 4

Note

You can also read article about Spring MVC below

Spring MVC 4 Example

Required Libraries
  1. JDK 6
  2. Eclipse 3.7
  3. Jackson 2.4.0
  4. Spring 4.0.5

Following jar must be in classpath

  1. jackson-annotations-2.4.0.jar
  2. jackson-core-2.4.0.jar
  3. jackson-databind-2.4.0.jar
  4. commons-logging-1.1.1.jar
  5. spring-aop-4.0.5.RELEASE.jar
  6. spring-beans-4.0.5.RELEASE.jar
  7. spring-context-4.0.5.RELEASE.jar
  8. spring-core-4.0.5.RELEASE.jar
  9. spring-expression-4.0.5.RELEASE.jar
  10. spring-web-4.0.5.RELEASE.jar
  11. spring-webmvc-4.0.5.RELEASE.jar

You can see the project structure below

Spring MVC 4 REST Example

Student.java

package com.student.bean;

public class Student {
   
private int        studId;
   
private String    name;
   
private int        age;

   
public int getStudId() {
       
return studId;
   
}

   
public void setStudId(int studId) {
       
this.studId = studId;
   
}

   
public String getName() {
       
return name;
   
}

   
public void setName(String name) {
       
this.name = name;
   
}

   
public int getAge() {
       
return age;
   
}

   
public void setAge(int age) {
       
this.age = age;
   
}

}

StudentController.java

In StudentController class, we have added @RestController annotation which removing the need to add @ResponseBody to each of your @RequestMapping methods.

@RequestMapping annotation is used for mapping web requests to corresponding handler classes and handler methods

package com.student.controller;

import java.util.ArrayList;
import java.util.List;

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;

import com.student.domain.Student;

@RestController
@RequestMapping
("/service/student/")
public class StudentController {
   

   
   
@RequestMapping(method = RequestMethod.GET, headers = "Accept=application/json")
   
public List<Student> getAllStudents() {
       
List<Student> Students = new ArrayList<Student>();

        Student student =
new Student();
        student.setName
("Rockey");
        Students.add
(student);

        student =
new Student();
        student.setName
("Jose");
        Students.add
(student);

       
return Students;
   
}
   
   
@RequestMapping(value = "/{id}", method = RequestMethod.GET, headers = "Accept=application/json")
   
public Student getStudentById(@PathVariable int id) {
       
Student student = null;
       
if (id == 1) {
           
student = new Student();
            student.setName
("Jose");
       
}else{
           
student = new Student();
            student.setName
("Invalid Student");
       
}
       
return student;
   
}
}
1 2 Next





Your email address will not be published. Required fields are marked *