REST服务| JSON响应格式

时间:2014-07-25 05:11:50

标签: json web-services rest jackson resteasy

我使用RESTEasy实现JAX-RS创建了一个REST Web服务。我有一个Employee POJO类,作为响应发送。响应采用json格式。问题是我将员工列表返回给调用者。以下是代码。

以下是员工POJO课程

@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;
}

}

以下是发送响应的服务方法。

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

响应格式生成如下:

[
{
    "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"
    }
}
]

但是,我希望格式为:

{
"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"
}]
}

我该怎么做?

1 个答案:

答案 0 :(得分:3)

您可以通过使用包装类来实现此目的。一个简单的例子是:

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;
    }
}

在响应中返回您的包装器对象,而不是普通的ArrayList