[英]Spring rest service to return multiple entity classes as xml response using @RestController
How can we return multiple entity classes as XML response in the spring rest service?我们如何在 spring rest 服务中返回多个实体类作为 XML 响应?
Example: I need a response like the following示例:我需要如下响应
<GetEmployeeById>
<Response>
<Status>0</Status>
<Message>Data found</Message>
</Response>
<Data>
<Employee>
<Id> 2 </Id>
<Name> Mani M </Name>
<Job> SOftware Developer </Job>
<Salary> 40000 </Salary>
</Employee>
</Data>
</GetEmployeeById>
Here, Response and Employee are separate entities.在这里,Response 和 Employee 是独立的实体。 I am getting the following XML response with my code.我的代码收到以下 XML 响应。 The issue here is, I am getting response node inside response node and employee node inside employee node.这里的问题是,我在响应节点内获取响应节点,在员工节点内获取员工节点。
<GetEmployeeById>
<Response>
<Response>
<Status>0</Status>
<Message>Data found</Message>
</Response>
</Response>
<Employee>
<Employee>
<Id>2</Id>
<Name>Mani M</Name>
<Job>SOftware Developer</Job>
<Salary>12000</Salary>
</Employee>
</Employee>
</GetEmployeeById>
Following is the java class where I am combining both the entity classes.以下是 java class 我将两个实体类结合在一起。
@XmlRootElement (name="GetEmployeesById")
public class GetEmployeesById implements Serializable{
private static final long serialVersionUID = 1L;
private List<Employee> Employee = new ArrayList<Employee>();
private List<Response> Response = new ArrayList<Response>();
public List<Employee> getEmployee() {
return Employee;
}
public void setEmployee(List<Employee> employee) {
Employee = employee;
}
public List<Response> getResponse() {
return Response;
}
public void setResponse(List<Response> Response) {
Response = Response;
}
}
Kindly help me with this.请帮我解决这个问题。
Your problem here is with List<CustomResponse>
as it is a List
, the parent </Response>
tag represents the List and child </Response>
represents the element in the List
您的问题在于List<CustomResponse>
因为它是一个List
,父</Response>
标记代表 List 而子</Response>
代表List
中的元素
In order to achieve your desired payload, Response
cannot be a List
and has to be CustomResponse
.为了实现您想要的有效负载, Response
不能是List
并且必须是CustomResponse
。
As for your data node, you might want to try creating a new class as per below至于您的数据节点,您可能想尝试创建一个新的 class 如下
@XmlRootElement(name = "Data")
public class Data implements Serializable {
private static final long serialVersionUID = 1L;
@XmlElement(name = "Employee")
private List<Employee> employees
}
You can then replace List<Employee>
with Data
in your GetEmployeesById
class然后,您可以用GetEmployeesById
class 中的Data
替换List<Employee>
Rename field and getters重命名字段和吸气剂
// private List<Employee> Employee = new ArrayList<Employee>();
private List<Employee> Data = new ArrayList<Employee>();
or add annotation @XmlElement(name="Data")
to field or getter (not sure)或将注释@XmlElement(name="Data")
添加到字段或吸气剂(不确定)
and change field (also getters)并更改字段(也是吸气剂)
// private List<Response> Response = new ArrayList<Response>();
private Response Response = new Response();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.