Java By Kiran
Java Training Institute in Pune
Java Training Institute in Pune
On this page we will learn how to use Spring 4 with REST Web Service to get JSON response. A Spring 4 Web Service class is annotated with @RestController that replaces the use of @Controller and @ResponseBody. To map the REST Web Service URL, use the annotation @RequestMapping. Web service method argument contains @RequestParam that has the attribute defaultValue that will assign a default value for request parameter whose value is not available in request.
To setup the environment, we can use Spring Boot for fast startup. WebApplicationInitializer is being used to replace web.xml settings. Spring configuration will be initialized by WebApplicationInitializer while on server startup.
Below softwares are needed to develop our Spring 4 + REST Web Service + JSON Example.
1. Java 6
2. Tomcat 7
Find the image view how the classes has been configured in eclipse for our spring 4 demo.
Spring Boot is the combination of Jar and Tomcat server. Spring Boot fetches all Jars required to run Spring 4 application. For developers, Spring Boot makes it easy to start development in easy and fast way. Spring Boot can be used with Gradle 1.11 or Maven 3.0. In our demo project we have used maven. In our demo, we have a separate tomcat in which we deploy our WAR file externally. For JSON, Jackson API will be used. For Jackson dependency jackson-databind is used. Find the maven dependency.
Spring configuration will not use XML approach. @Configuration supports a class to define the spring configuration for component and beans used in the application.
In servlet 3.0, now java web application has capability to run its application without using web.xml. In spring 3.1 onwards WebApplicationInitializer is used in place of web.xml and continue to Spring 4. In our demo all the web setting will be done in a class which will implement WebApplicationInitializer. On startup, server looks for WebApplicationInitializer. If server finds it in the application, then server starts the application using the settings defined in WebApplicationInitializer implementing class.
We have created a sample component for the example. This has a method that will return an object Person which will be changed in JSON format in response. Find the Person class which will be retuned as JSON response.
Find the interface for component class.
Implement the method of interface.
Finally we implement our web service class which will be exposed for REST service. The class will be annotated with @RestController . Spring 4 has introduced @RestController annotation to replace @Controller and @ResponseBody. The annotation @RequestMapping can be used at class level as well as method level for REST Web Service URL mapping.
To run demo, we need to use Maven commands as follows.
1. To set classpath in eclipse, run the maven command mvn eclipse:eclipse from the project root directory using command prompt.
2. To create WAR file, run the command mvn clean package. We will get WAR file in target directory
3. Deploy war in Tomcat and hit the URL
Output will be displayed as below
Find the print screen of the output.