Struts 2 and JSON Integration using Annotation Example
In this Struts 2 tutorial, we will learn to get JSON response using Annotation
. In the previous tutorial you have seen we get the JSON response using struts.xml but if you are using Annotation
, you do not need to create the struts.xml file.
Similar Post: Struts 2 and JSON Integration Example
Get the JAR Dependencies
To get the JSON response, you must have these JAR in your build path.
<dependencies>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.20</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-convention-plugin</artifactId>
<version>2.3.20</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-json-plugin</artifactId>
<version>2.3.20</version>
</dependency>
</dependencies>
Note: To get the JSON response you must need to set
"json-default"
as parent package and result type isjson
.
Project Structure in NetBeans
Convention Plugin
The Convention Plugin is bundled with Struts since 2.1 and replaces the Codebehind Plugin and Zero Config plugins. It provides the following features :
- Action location by package naming conventions
- Result (JSP, FreeMarker, etc) location by naming conventions
- Class name to URL naming convention
- Package name to namespace convention
- Action name overrides using annotations
- Namespace overrides using annotations
- XWork package overrides using annotations
Set Parent Package
Using annotation set the package as json-default
to support the JSON.
@ParentPackage("json-default")
Set Result Type
@Result(name="success", type="json")
Define filter in web.xml
Define the struts 2 filters in web.xml and pass the action class by defining actionPackages.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>actionPackages</param-name>
<param-value>org.websparrow.action</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Action Class
In this class data converted into JSON format.
package org.websparrow.action;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import com.opensymphony.xwork2.ActionSupport;
@Result(name = "success", type = "json")
@ParentPackage("json-default")
public class StrutsJsonAnnotationAction extends ActionSupport {
private static final long serialVersionUID = 3516335522937177571L;
private String name = "Narendra Modi";
private String designation = "Prime Minister of India";
private String dob = "17 September 1950";
private String[] education = {"MA", "BA"};
private List<String> favBooks = new ArrayList<String>();
private Map<String, String> assumedOffice = new HashMap<String, String>();
public StrutsJsonAnnotationAction() {
favBooks.add("Ramayan");
favBooks.add("Geeta");
assumedOffice.put("President", "Pranab Mukherjee");
assumedOffice.put("Preceded by", "Manmohan Singh");
}
@org.apache.struts2.convention.annotation.Action("/india")
@Override
public String execute() {
return SUCCESS;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
public String[] getEducation() {
return education;
}
public void setEducation(String[] education) {
this.education = education;
}
public List<String> getFavBooks() {
return favBooks;
}
public void setFavBooks(List<String> favBooks) {
this.favBooks = favBooks;
}
public Map<String, String> getAssumedOffice() {
return assumedOffice;
}
public void setAssumedOffice(Map<String, String> assumedOffice) {
this.assumedOffice = assumedOffice;
}
}
Output:
To run the application start your server and hit this URL in address bar localhost:8090/Struts2JsonAnnotationExample/india.action. To get the JSON data in arranged form use the Chrome Postman.
Download Source Code – struts2-and-json-integration-using-annotation-example.zip