'Struts2 request PUT parameter on body

i want to add parameter for HTTP Request PUT on the body, but i only can do like this

http://localhost:9996/Events/employee/{id}.json?name={name}

so how to put name on body?

here my controller

public class EmployeeController implements ModelDriven<Object>{
private static final long serialVersionUID = 1L;
private String id;
private String name;
private Object model;  
private EmployeeRepository employeeRepository = new EmployeeRepository();

//GET
public HttpHeaders index() { 
    model = employeeRepository.findAllEmployee(); 
    return new DefaultHttpHeaders("index").disableCaching();
}

// GET
public HttpHeaders show() { 
    model = employeeRepository.getEmployeeById(id);
    return new DefaultHttpHeaders("show").disableCaching();
}

//POST
public HttpHeaders create() { 
    Integer empId = Integer.parseInt(id);
    Employee emp = new Employee(empId, name, "PQR");
    employeeRepository.addEmployee(emp);
    model = employeeRepository.findAllEmployee();
    return new DefaultHttpHeaders("create").disableCaching();
}

//PUT
public HttpHeaders update() {  
    Integer empId = Integer.parseInt(id);
    Employee emp = new Employee(empId, name, "PQR");
    employeeRepository.updateEmployee(emp);
    model = employeeRepository.findAllEmployee();
    return new DefaultHttpHeaders("update").disableCaching();
}

//DELETE
public HttpHeaders destroy() {   
    employeeRepository.deleteEmployee(id); 
    model = employeeRepository.findAllEmployee();
    return new DefaultHttpHeaders("destroy").disableCaching();
}

public  String  getId()                 { return id; } 
public  void    setId(String id)        { this.id = id; } 
public  String  getName()               { return name; } 
public  void    setName(String name)    { this.name = name; } 
public  Object  getModel()              { return model; }


Solution 1:[1]

You need to add the JSON Interceptor for your action and it should look something like the code below.

<package name="movies" extends="rest-default, json-default">

        <interceptors>
            <interceptor name="json" class="org.apache.struts2.json.JSONInterceptor"/>

            <interceptor-stack name="jsonStack">
                <interceptor-ref name="json"/>
                <interceptor-ref name="defaultStack"/>
            </interceptor-stack>
        </interceptors>

<action name="movies" class="com.struts.movie.action.MoviesController">
            <interceptor-ref name="jsonStack"/>

            <result type="json">
                <!-- declarations for mappings from Request JSON Body goes here-->
                <param name="root">movie</param>
            </result>
        </action>

 </package>

One catch over here is, which I encountered, for a simple JSON Structure, like the one below,

{
    "hello": {
        "name":"kav"
    },
    "uid":"fdsfds"
}

where hello was intended to map to a POJO class Hello.java which was declared as a variable (private Hello hello; with its public getters and setters) in the controller. And uid was intended to map to a String variable uid declared in the controller (as private String uid; with its public getters and setters). This got mapped exactly without hello being mentioned in the <result type="json"> tag, like <param name="root">hello</param>.

In the case of movie, it wasn't so, as it had nested objects, like the code below,

public class Movie{

    private String movieName;
    private String movieID;
    .... other variables
    private List<Actor> actors;
    private List<Review> reviews;

   // Declarations of constructor, Getters & Setters
}

Not sure why it happens so.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kavin Raju S