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.


Note: To get the JSON response you must need to set "json-default" as parent package and result type is json.

Project Structure in NetBeans

Struts 2 and JSON Integration using Annotation Example

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.


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="" xmlns="" xsi:schemaLocation="" id="WebApp_ID" version="3.1">

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")
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() {


        assumedOffice.put("President", "Pranab Mukherjee");
        assumedOffice.put("Preceded by", "Manmohan Singh");

    public String execute() {
        return SUCCESS;

    public String getName() {
        return name;

    public void setName(String 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) { = 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;



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.

Struts 2 and JSON Integration using Annotation Example

Similar Posts

About the Author

I like sharing my experiments and ideas with everyone by writing articles on the latest technological trends.