Manav Bhanot
Manav Bhanot

Reputation: 113

REST Service | JSON response format

I have created a REST Web Service using RESTEasy implentation of JAX-RS. I have an Employee POJO class which is sent as response. The response is in the json format. The problem is that I am returning the List of Employees back to the caller. Below is the code.

Below is the Employee POJO class

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"employeeId",
"name",
"role"
})
@XmlRootElement(name = "employee")
public class Employee {

@XmlElement(required = true)
protected String employeeId;
@XmlElement(required = true)
protected Name name;
@XmlElement(required = true)
protected String role;

/**
 * Gets the value of the employeeId property.
 * 
 * @return
 *     possible object is
 *     {@link String }
 *     
 */
public String getEmployeeId() {
    return employeeId;
}

/**
 * Sets the value of the employeeId property.
 * 
 * @param value
 *     allowed object is
 *     {@link String }
 *     
 */
public void setEmployeeId(String value) {
    this.employeeId = value;
}

/**
 * Gets the value of the name property.
 * 
 * @return
 *     possible object is
 *     {@link Name }
 *     
 */
public Name getName() {
    return name;
}

/**
 * Sets the value of the name property.
 * 
 * @param value
 *     allowed object is
 *     {@link Name }
 *     
 */
public void setName(Name value) {
    this.name = value;
}

/**
 * Gets the value of the role property.
 * 
 * @return
 *     possible object is
 *     {@link String }
 *     
 */
public String getRole() {
    return role;
}

/**
 * Sets the value of the role property.
 * 
 * @param value
 *     allowed object is
 *     {@link String }
 *     
 */
public void setRole(String value) {
    this.role = value;
}

}

Below is the service method that sends the response.

@GET
@Path("/json/employees/")
@Produces("application/json")
public List<Employee> listEmployeesJSON() {
    return new ArrayList<Employee>(employees.values());

The response format generate is as below :

[
{
    "employee": {
        "employeeId": 876,
        "name": {
            "salutation": "Mr.",
            "firstName": "Manav",
            "lastName": "Bhanot"
        },
        "role": "Senior Engineer"
    }
},
{
    "employee": {
        "employeeId": 923,
        "name": {
            "salutation": "Mr.",
            "firstName": "Saumya",
            "lastName": "Mittal"
        },
        "role": "Senior Engineer"
    }
}
]

However, I want the format to be as :

{
"employee": [{
    "employeeId": 876,
    "name": {
        "salutation": "Mr.",
        "firstName": "Manav",
        "lastName": "Bhanot"
    },
    "role": "Senior Engineer"
},{
    "employeeId": 923,
    "name": {
        "salutation": "Mr.",
        "firstName": "Saumya",
        "lastName": "Mittal"
    },
    "role": "Senior Engineer"
}]
}

How can I do this ?

Upvotes: 1

Views: 1482

Answers (1)

Sam Berry
Sam Berry

Reputation: 7844

You can accomplish this by utilizing a wrapper class. A simple example is:

public class EmployeeWrapper {
    private final ArrayList<Employee> employees;

    @JsonCreator
    public EmployeeWrapper(@JsonProperty("employees") ArrayList<Employee> employees) {
        this.employees = employees;
    }

    public ArrayList<Employee> getEmployees() {
        return employees;
    }
}

Return your wrapper object in the response instead of the plain ArrayList.

Upvotes: 3

Related Questions