Spring Rest service Hello world with XML

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

Project structure


SpringRestHelloWorldXML_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>SpringRestHelloWorldXML</groupId>
  5.     <artifactId>SpringRestHelloWorldXML</artifactId>
  6.     <packaging>war</packaging>
  7.     <version>0.0.1-SNAPSHOT</version>
  8.     <name>SpringRestHelloWorldXML 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.     </dependencies>
  32.     <build>
  33.         <finalName>SpringRestHelloWorldXML</finalName>
  34.     </build>
  35. </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>SpringRestHelloWorldXML</groupId>
	<artifactId>SpringRestHelloWorldXML</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>SpringRestHelloWorldXML 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>
	</dependencies>
	<build>
		<finalName>SpringRestHelloWorldXML</finalName>
	</build>
</project>

We have added dependencies for Spring web ,spring web mvc 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. import javax.xml.bind.annotation.XmlAccessType;
  4. import javax.xml.bind.annotation.XmlAccessorType;
  5. import javax.xml.bind.annotation.XmlElement;
  6. import javax.xml.bind.annotation.XmlRootElement;
  7.  
  8. @XmlRootElement
  9. @XmlAccessorType(XmlAccessType.FIELD)
  10. public class HelloWorld {
  11.     @XmlElement
  12.     private String message;
  13.  
  14.     public String getMessage() {
  15.         return message;
  16.     }
  17.  
  18.     public void setMessage(String message) {
  19.         this.message = message;
  20.     }
  21.  
  22. }
package com.kb.rest.model;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class HelloWorld {
	@XmlElement
	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

@XmlRootElement – specifies the root tag of each HelloWorld record in xml.

@XmlElement – specifies the child element for each attribute of HelloWorld record.

@XmlAccessorType(XmlAccessType.FIELD) – specifies that fields are considered to be serialized

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/SpringRestHelloWorldXML/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/SpringRestHelloWorldXML/message

SpringRestHelloWorldXML_output

Download this project SpringRestHelloWorldXML.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