0

I am trying to develop a rest service in Struts2. To do that we can implement via struts2-rest-plugin or struts2-json-plugin.Just wanted to understand that how struts2-rest-plugin implementation is different from struts2-json-plugin. Following is the working code for struts2-json-plugin

struts.xml

<package name="default" extends="json-default">
    <action name="getEmployeeDetails" class="com.world.employee.GetEmployeeDetails">
            <interceptor-ref name="json"/> 
            <result type="json">
                <param name="excludeNullProperties">true</param>
                <param name="noCache">true</param>
            </result>
    </action>
</package>

GetEmployeeDetails.java

class Employee{
    String name;
    String department;
    String age;
    String branch;
}
public class  GetEmployeeDetails implements Action {

   private String empID;
   private Employee emp; 

  @Override
  public String execute() throws Exception {
      emp=new Employee();
      emp.name="Alex";
      emp.department="Navy";
      emp.age="40";
      emp.branch="Atlanta";



    if(emp == null) {
      return Action.ERROR;
    }
    return Action.SUCCESS;
  } 

}

Consuming from postman:

URL : http://localhsot:8080/SampleRestExample/getEmployeeDetails
Body : 
{
              "empID":"5468",

}

1 Answers1

0

The JSON plugin provides JSON serialization of an action: https://struts.apache.org/plugins/json/

The REST plugin does RESTful mapping (more or less), error handling, serialization, etc.: https://struts.apache.org/plugins/rest/

Reading the docs for the plugins provides a pretty clear understanding of the differences.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302