Struts 2 + REST Web Service Integration Example

By Arvind Rai, January 04, 2015
This page will provide the Struts 2 and REST web service integration example. Struts 2 provide REST plugin to implement REST web service. Parent package should be rest-default. Conventionally action class name is written with Controller suffix. Struts 2 REST web service supports different content-type response. To get response as JSON, use json extension and to get XML response, use xml extension. In the example we have an employee repository REST web service. For the given employee id in REST URL, employee profile is retuned by the application.

Required Software to Run Application

To run the example, we need below software.
1. Java 7
2. Tomcat 7
3. Eclipse
4. Maven

Project Structure in Eclipse

Find the project structure in eclipse for struts 2 and REST web service integration.
Struts 2 + REST Web Service Integration Example

pom.xml: Struts 2 REST Plugin

To resolve struts 2 REST API, we need the below plugin in our pom.xml
<project xmlns="" xmlns:xsi=""
To support struts 2 rest plugin, we need to resolve dependency for struts 2 convention plugin.

struts.xml : Configure Convention Plugin

Find the struts.xml in which struts convention plugin is configured.
<?xml version="1.0" encoding="UTF-8" ?>
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    <constant name="struts.convention.action.suffix" value="Controller"/>
    <constant name="struts.convention.action.mapAllMatches" value="true"/>
    <constant name="struts.convention.default.parent.package" value="rest-default"/>
    <constant name="struts.convention.package.locators" value="rest"/>
Find the description for the convention plugin being used.

<constant name="struts.convention.action.suffix" value="Controller"/>: Defines a suffix for action class. Conventionally action class for REST web service has Controller suffix and this suffix will not be included in URL.

<constant name="struts.convention.action.mapAllMatches" value="true"/>: mapAllMatches can be true or false.

<constant name="struts.convention.default.parent.package" value="rest-default"/>: To support REST web service, struts 2 uses rest-default as parent package.

<constant name="struts.convention.package.locators" value="rest"/>: Locator is the last word of the package within which controller action class resides. In our class, it is rest.

Create Employee Repository

For the demo application, we are creating an employee repository. First find the employee class.
public class Employee {
	private Integer id;
	private String name;
	private String company;
	public Employee(Integer id, String name, String company){ =id; = name; = company;
	public Integer getId() {
		return id;
	public void setId(Integer id) { = id;
	public String getName() {
		return name;
	public void setName(String name) { = name;
	public String getCompany() {
		return company;
	public void setCompany(String company) { = company;
Now find the repository class with some sample data.
import java.util.HashMap;
import java.util.Map;
public class EmployeeRepository {
	private static Map<String,Employee> map = new HashMap<String,Employee>(); 
	public EmployeeRepository(){
		map.put("111", new Employee(111, "Ram", "ABC"));
		map.put("222", new Employee(222, "Shyam", "EFG"));
		map.put("333", new Employee(333, "Mohan", "XYZ"));
	public  Employee getEmployeeById(String id){
		return map.get(id);
	public  Map<String,Employee> findAllEmployee(){
		return map;

Create Controller Action Class: Implement ModelDriven

Find the controller class. The class will extend ModelDriven and need to override getModel () method. Whenever a REST URL is hit, this is the method which returns response.
import java.util.Map;
import com.opensymphony.xwork2.ModelDriven;
public class EmployeeController implements ModelDriven<Object>{
	private static final long serialVersionUID = 1L;
	private String id;
	private Object model;
	private EmployeeRepository employeeRepository = new EmployeeRepository();
	private static Map<String,Employee> map;
		map = employeeRepository.findAllEmployee();
	public HttpHeaders index() {
		model = map;
		return new DefaultHttpHeaders("index").disableCaching();
	public String add(){
		Integer empId = Integer.parseInt(id);
		Employee emp = new Employee(empId,"Ramesh", "PQR");
		model = emp;
		return "SUCCESS";
	public String getId() {
		return id;
	public void setId(String id) {
		model = employeeRepository.getEmployeeById(id); = id;
	public Object getModel() {
		return model;
1. URL for index(): The REST web service URL can be as below for the index() method.


Look at the URL. employee has been taken from EmployeeController class. Suffix is ignored. The default method is index(). To fetch JSON response, use json extension and to fetch xml response, use xml extension.

2. URL for id: Now find other URLs for id setter and getter.


To pass any input value, there must an id property and we need to create setter and getter method for the id.

3. URL for add(): To call our own method, create a method like add(). This method will be called after id setter and getter. The value 555 in the URL will be assigned to id. The URL will be as below.


This url will call add() method and return data assigned to model.

web.xml for Application

Find the web.xml for struts 2.
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "" >
  <display-name>Struts 2 REST Web Service</display-name>


1. JSON Response of the URL http://localhost:8080/struts2rest-1/employee/111.json
XML response of the URL http://localhost:8080/struts2rest-1/employee/111.xml
Find the screenshot.
Struts 2 + REST Web Service Integration Example
2. JSON Response of the URL http://localhost:8080/struts2rest-1/employee/555/add.json
XML response of the URL http://localhost:8080/struts2rest-1/employee/555/add.xml
Find the screenshot.
Struts 2 + REST Web Service Integration Example
Now we are done. Happy Learning.


REST Plugin

Download Source Code


©2024 | Privacy Policy | Contact Us