泽西JAX-RS客户端XML到java.util.List的反序列化 [英] Jersey JAX-RS Client XML to java.util.List deserialization
问题描述
我正在尝试访问JAX-RS服务(球衣实现),该服务以XML格式返回了一个Employees的java.util.list.
I am trying to access a JAX-RS Service (jersey implementation) which is returning me a java.util.list of Employees in XML format.
Service方法签名如下:
The Service method signature looks like this:
@GET
@Path("/getEmployeeListXML")
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.APPLICATION_XML)
public List<EmployeeXML> getEmployeeListXML(@QueryParam("id") String id){
//Some code which returns a List<EmployeeXML>
}
返回的XML格式如下:
The format of the XML returned is like this:
<employeeXMLs>
<employeeXML>
<empId>1</empId>
<empName>John</empName>
<empAge>35</empAge>
<empSex>Male</empSex>
</employeeXML>
<employeeXML>
<empId>2</empId>
<empName>Lisa</empName>
<empAge>23</empAge>
<empSex>Female</empSex>
</employeeXML>
</employeeXMLs>
要从我的jersey客户端访问此文件,我正在使用以下代码:
For accessing this from my jersey Client, I am using this code:
List<EmployeeXML> empListXML = (List<EmployeeXML>)service.path("rest").path("GetService").path("getEmployeeListXML").accept(MediaType.APPLICATION_XML).get(EmployeeXML.class);
这是不正确的,因为返回类型应该是一个列表,但是在get方法中,目前我正在尝试检索单个对象.我不确定如何在此处从客户端检索列表:(
This is not correct since the return type should be a list but in the get method, presently I am trying to retrieve a single object. I am not sure how to retrieve the List from the client here :(
我遇到此异常:
unexpected element (uri:"", local:"employeeXMLs"). Expected elements are <{}employeeListXML>,<{}employeeXML>
请帮助我完成这项工作.
Please help me out to make this work.
谢谢
推荐答案
您需要使用'supertype token'来定义客户端类中的返回类型:
You need to use a 'supertype token' to define the return type in your client class:
List<EmployeeXML> empListXML = service
.path("rest")
.path("GetService")
.path("getEmployeeListXML")
.accept(MediaType.APPLICATION_XML)
.get(new GenericType<List<EmployeeXML>>() {});
必须使用超类型令牌,才能保留" Jersey在反序列化服务器响应时将使用的通用参数信息.
The supertype token is required in order to 'retain' generic parameter information that Jersey will use when deserializing the server response.
这篇关于泽西JAX-RS客户端XML到java.util.List的反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!