REST服务| JSON响应格式
我使用JAX-RS的RESTEasy实现创建了一个RESTWeb服务。我有一个Employee POJO类,它作为响应发送。响应是json格式的。问题是我正在将员工名单返回给打电话的人。下面是代码 下面是Employee POJO类REST服务| JSON响应格式,json,web-services,rest,jackson,resteasy,Json,Web Services,Rest,Jackson,Resteasy,我使用JAX-RS的RESTEasy实现创建了一个RESTWeb服务。我有一个Employee POJO类,它作为响应发送。响应是json格式的。问题是我正在将员工名单返回给打电话的人。下面是代码 下面是Employee POJO类 @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = { "employeeId", "name", "role" }) @XmlRootElement(name = "emplo
@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"
},{
"employeeId": 923,
"name": {
"salutation": "Mr.",
"firstName": "Saumya",
"lastName": "Mittal"
},
"role": "Senior Engineer"
}]
}
如何实现这一点?您可以通过使用包装器类来实现这一点。一个简单的例子是:
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;
}
}
公共类EmployeeWrapper{
私人最终ArrayList员工;
@JsonCreator
公共EmployeeWrapper(@JsonProperty(“employees”)ArrayList employees){
这是。雇员=雇员;
}
公共阵列列表getEmployees(){
返回员工;
}
}
在响应中返回包装器对象,而不是普通的
ArrayList
它会给我以下输出[{“employeeId”:“2”,“employeeName”:“Justin”,“job”:“Business Analyst”},{“employeeId”:“1”,“employeeName”:“Fabrizio”,“job”:“Software Engineer”}Ok。如果输出有问题,你能更具体一点吗?嗨,山姆,输出接近我想要的。输出中没有外部对象employee:。我已将服务方法返回类型修改为:returnnewEmployeeWrapper(newArrayList(employees.values()).getEmployee();我不确定我是否正确使用了你的解决方案。我现在正在想办法。我还有一个问题。这是可以推广的,还是我必须为每个发送arraylist作为响应的情况创建一个包装器?
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;
}
}