Spring Rest service Hello world with JSON

Create a new Maven Web project in eclipse (Refer Spring MVC Hello World project for the same)

Project structure


SpringRestHelloWorldJSON_proj_structure

Step 1

Update pom.xml with below dependencies

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3.     <modelVersion>4.0.0</modelVersion>
  4.     <groupId>SpringRestHelloWorldJSON</groupId>
  5.     <artifactId>SpringRestHelloWorldJSON</artifactId>
  6.     <packaging>war</packaging>
  7.     <version>0.0.1-SNAPSHOT</version>
  8.     <name>SpringRestHelloWorldJSON Maven Webapp</name>
  9.     <url>http://maven.apache.org</url>
  10.     <properties>
  11.         <org.springframework.version>4.2.0.RELEASE</org.springframework.version>
  12.     </properties>
  13.     <dependencies>
  14.         <dependency>
  15.             <groupId>junit</groupId>
  16.             <artifactId>junit</artifactId>
  17.             <version>3.8.1</version>
  18.             <scope>test</scope>
  19.         </dependency>
  20.         <dependency>
  21.             <groupId>org.springframework</groupId>
  22.             <artifactId>spring-web</artifactId>
  23.             <version>${org.springframework.version}</version>
  24.         </dependency>
  25.  
  26.         <dependency>
  27.             <groupId>org.springframework</groupId>
  28.             <artifactId>spring-webmvc</artifactId>
  29.             <version>${org.springframework.version}</version>
  30.         </dependency>
  31.         <!-- Jackson JSON -->
  32.         <dependency>
  33.             <groupId>com.fasterxml.jackson.core</groupId>
  34.             <artifactId>jackson-databind</artifactId>
  35.             <version>2.8.5</version>
  36.         </dependency>
  37.  
  38.     </dependencies>
  39.     <build>
  40.         <finalName>SpringRestHelloWorldJSON</finalName>
  41.     </build>
  42. </project>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>SpringRestHelloWorldJSON</groupId>
	<artifactId>SpringRestHelloWorldJSON</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>SpringRestHelloWorldJSON Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<properties>
		<org.springframework.version>4.2.0.RELEASE</org.springframework.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>
		<!-- Jackson JSON -->
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.8.5</version>
		</dependency>

	</dependencies>
	<build>
		<finalName>SpringRestHelloWorldJSON</finalName>
	</build>
</project>

We have added dependencies for Spring web ,spring web mvc,JSON processor and Junit in the above pom file.

Step 2

Update web.xml file with Dispatcher servlet

we have defined a dispatcher servlet in web.xml and mapped it by the URL pattern “/”

So just like any other servlet in web application,any request matching with the given pattern i.e “/” will be redirected to “Dispatcher servlet”.

  1. <web-app id="WebApp_ID" version="2.4"
  2.     xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
  4. http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  5.  
  6.     <display-name>Archetype Created Web Application</display-name>
  7.     <servlet>
  8.         <servlet-name>mvc-dispatcher</servlet-name>
  9.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  10.         <init-param>
  11.             <param-name>contextConfigLocation</param-name>
  12.             <param-value>
  13.             /WEB-INF/spring-mvc.xml
  14.             </param-value>
  15.         </init-param>
  16.         <load-on-startup>1</load-on-startup>
  17.     </servlet>
  18.  
  19.     <servlet-mapping>
  20.         <servlet-name>mvc-dispatcher</servlet-name>
  21.         <url-pattern>/</url-pattern>
  22.     </servlet-mapping>
  23.  
  24.     <context-param>
  25.         <param-name>contextConfigLocation</param-name>
  26.         <param-value>/WEB-INF/spring-mvc.xml</param-value>
  27.     </context-param>
  28.  
  29.     <listener>
  30.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  31.     </listener>
  32. </web-app>
<web-app id="WebApp_ID" version="2.4"
	xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

	<display-name>Archetype Created Web Application</display-name>
	<servlet>
		<servlet-name>mvc-dispatcher</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>
            /WEB-INF/spring-mvc.xml
            </param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>mvc-dispatcher</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/spring-mvc.xml</param-value>
	</context-param>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
</web-app>

We have also provided the spring configuration file name to create and load the spring beans while starting the server.

Step 3

Create a Model class which represents the data with JAXB annotations

  1. package com.kb.rest.model;
  2.  
  3. public class HelloWorld {
  4.    
  5.     private String message;
  6.  
  7.     public String getMessage() {
  8.         return message;
  9.     }
  10.  
  11.     public void setMessage(String message) {
  12.         this.message = message;
  13.     }
  14.  
  15. }
package com.kb.rest.model;

public class HelloWorld {
	
	private String message;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

}

We have created a HelloWorld class with message to represent the data

This will be automatically mapped to JSON by Spring using Jackson processor.

Step 4

Create the Controller which will have the URL mapping method

  1. package com.kb.rest.controllers;
  2.  
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.ResponseBody;
  6.  
  7. import com.kb.rest.model.HelloWorld;
  8.  
  9. @Controller
  10. public class HelloWorldController {
  11.     @RequestMapping("/message")
  12.     public @ResponseBody HelloWorld getMessage(){
  13.         HelloWorld helloWorld = new HelloWorld();
  14.         helloWorld.setMessage("Hello World");
  15.         return helloWorld;
  16.     }
  17.  
  18. }
package com.kb.rest.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kb.rest.model.HelloWorld;

@Controller
public class HelloWorldController {
	@RequestMapping("/message")
	public @ResponseBody HelloWorld getMessage(){
		HelloWorld helloWorld = new HelloWorld();
		helloWorld.setMessage("Hello World");
		return helloWorld;
	}

}

We have created the Controller class above which will act as a Rest service in Spring.

How Spring controller acts as a Rest service ?

We have @ResponseBody before the return type of a method in method signature which indicates to Spring that ,the returned value from this method will not be a view rather it has to be read from the response body.

Hence any one can call this method with the appropriate end point as below

http://localhost:8080/SpringRestHelloWorldJSON/message

We can also use @RestController directly instead of @Controller, in that case we don’t need to use @ResponseBody in each method.

Note: @RestController is supported in Spring 4 and above

Step 5

Build and deploy the project

Step 6

Let’s see the output

access the below URL

http://localhost:8080/SpringRestHelloWorldJSON/message

SpringRestHelloWorldJSON_output

Download this project SpringRestHelloWorldJSON.zip

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on