In this tutorial, we will learn to write APIs capable of returning JSON
representations of resources.
Step 1 :- Open Eclipse and Create Dynamic Web Project named SpringRESTExample
Step 2:- Make sure you use Target Runtime as Apache Tomcat 7.0 and Dynamic web module version as 3.0.
Step 3 :- copy below jars to WEB-INF/lib folder.
STEP 5:- Map Spring MVC in /WebContent/WEB-INF/web.xml file as below
If you are using @RestController you don't need to specify @ResponseBody
STEP 6:- Create Controller Class
STEP 7:- Create Model Class
STEP 8 :- Run your project enter below URL in your browser
Now when you hit the URL : http://localhost:8080/SpringRESTExample/rest/Employees you will get this result.
If you hit the URL : http://localhost:8080/SpringRESTExample/rest/Employee/25 you will get this result.
Keep visiting TutorialsDesk for more tutorials and practical programming examples on Spring MVC. Hope we are able to explain you Spring REST Hello World JSON Example, if you have any questions or suggestions please write to us using contact us form.
Please share us on social media if you like the tutorial.
Step 1 :- Open Eclipse and Create Dynamic Web Project named SpringRESTExample
Step 2:- Make sure you use Target Runtime as Apache Tomcat 7.0 and Dynamic web module version as 3.0.
Step 3 :- copy below jars to WEB-INF/lib folder.
- commons-logging-1.2.jar
- jackson-annotations-2.6.0.jar
- jackson-core-2.6.0.jar
- jackson-databind-2.6.0.jar
- spring-aop-4.1.4.RELEASE.jar
- spring-beans-4.1.4.RELEASE.jar
- spring-context-4.1.4.RELEASE.jar
- spring-core-4.1.4.RELEASE.jar
- spring-expression-4.1.4.RELEASE.jar
- spring-web-4.1.4.RELEASE.jar
- spring-webmvc-4.1.4.RELEASE.jar
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.tutorialsdesk.controller" /> <mvc:annotation-driven/> <bean name="viewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver"/> <bean name="jsonTemplate" class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"/> </beans>You will need to configure viewName “jsonTemplate” as bean of type MappingJackson2JsonView. And you will need to configure view resolver of type BeanNameViewResolver. This way viewName “jsonTemplate” will be matched with MappingJackson2JsonView and parsed JSON response will be returned to client.
STEP 5:- Map Spring MVC in /WebContent/WEB-INF/web.xml file as below
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>SpringRESTExample</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
Using @ResponseBody Annotation
This technique is simple and easy. You have to include only jackson dependencies into classpath of your application and spring will register Jackson2JsonMessageConverter class automatically into context. Whenever you ask for a resource from REST API and provide http header “accept: application/json“, you will get back the json representation of resource.Here @RestController = @Controller + @ResponseBody
If you are using @RestController you don't need to specify @ResponseBody
STEP 6:- Create Controller Class
- package com.tutorialsdesk.controller
- Filename: EmployeeController.java
package com.tutorialsdesk.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.tutorialsdesk.model.Employee; @RestController @RequestMapping("rest") public class EmployeeController { @RequestMapping(value = "/Employees", method = RequestMethod.GET,headers="Accept=application/json") public List<Employee> getCountries(){ List<Employee> listOfEmployees = new ArrayList<Employee>(); listOfEmployees=createEmployeeList(); return listOfEmployees; } @RequestMapping(value = "/Employee/{id}", method = RequestMethod.GET,headers="Accept=application/json") public Employee getEmployeeById(@PathVariable int id){ List<Employee> listOfEmployees = new ArrayList<Employee>(); listOfEmployees=createEmployeeList(); for (Employee Employee: listOfEmployees) { if(Employee.getEmpId()==id) return Employee; } return null; } public List<Employee> createEmployeeList(){ List<Employee> listOfEmployees = new ArrayList<Employee>(); listOfEmployees.add(new Employee(29, "Prakash")); listOfEmployees.add(new Employee(30, "Rambir")); listOfEmployees.add(new Employee(35, "Sachin")); listOfEmployees.add(new Employee(25, "Martin")); return listOfEmployees; } }
STEP 7:- Create Model Class
- package com.tutorialsdesk.model
- Filename: Employee.java
package com.tutorialsdesk.model; public class Employee { private String name; private int empId; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getEmpId() { return empId; } public void setEmpId(int empId) { this.empId = empId; } public Employee(int empId, String name){ this.name = name; this.empId = empId; } }
STEP 8 :- Run your project enter below URL in your browser
Now when you hit the URL : http://localhost:8080/SpringRESTExample/rest/Employees you will get this result.
[{"name":"Prakash","empId":29},{"name":"Rambir","empId":30},{"name":"Sachin","empId":35},{"name":"Martin","empId":25}]
If you hit the URL : http://localhost:8080/SpringRESTExample/rest/Employee/25 you will get this result.
{"name":"Martin","empId":25}
Keep visiting TutorialsDesk for more tutorials and practical programming examples on Spring MVC. Hope we are able to explain you Spring REST Hello World JSON Example, if you have any questions or suggestions please write to us using contact us form.
Please share us on social media if you like the tutorial.
Blogger Comment
Facebook Comment