Marshal / Un marshal使用JAXB在Jersey JAX-RS中列出对象 [英] Marshal/Un marshal List objects in Jersey JAX-RS using JAXB
问题描述
早上好。今天早上,当我通过泽西实体提供商 MessageBodyReader
和 MessageBodyWriter
时,我遇到了以下问题。
Good Morning. Today morning when I am going through Jersey Entity providers MessageBodyReader
s and MessageBodyWriter
s I came across the following problem.
我想写一个资源方法和客户端,它返回一个自定义对象列表,媒体类型是 application / xml
。所以我想使用JAXB(我是JAXB的新手)。我能够通过编写自己的扩展 MessageBodyReader
和 MessageBodyWriter
来实现这一目标。但我害怕跟随的方式。看看我实施的方式:
I want to write a resource method and client that returns a list of custom objects and media type is application/xml
. So I would like to use JAXB (I am new to JAXB). I was able to achieve this by writing my own extended MessageBodyReader
and MessageBodyWriter
. But I am afraid of the way I am following. Just look at the way I implemented:
资源方法:
@Path("productlist/xml")
@GET
public RetObjects getProductsXml(){
List<Product> pList = new ArrayList<Product>();
pList.add(new Product("1","Dell latitude E6000",2900,500));
pList.add(new Product("2","Xperia Z2",549,400));
RetObjects obj = new RetObjects();
obj.setObject(pList);
return obj;
}
我的自定义对象:
@Entity
@Table (name="PRODUCT")
@XmlRootElement(name="product")
public class Product {
@Id
@Column(name = "CODE")
private String code;
...
// rest of the fields, constructors, getters and setters
}
包装我的自定义对象列表的对象:
Object that wraps my list of custom object:
@XmlRootElement(name = "products")
@XmlAccessorType (XmlAccessType.FIELD)
public class RetObjects {
@XmlElement(name = "product")
private List<Product> object = null;
public List<Product> getObject() {
return object;
}
public void setObject(List<Product> object) {
this.object = object;
}
}
MessageBodyReader / Writer
直接使用Jaxb unmarshaller和marshaller而不是 RetObjects
对象。
MessageBodyReader/Writer
are straight forward just using Jaxb unmarshaller and marshaller over the RetObjects
object.
使用此实现它正如预期的那样正常工作,我能够获取 RetObjects
在客户端完美地包装产品列表。
With this implementation it is working fine as expected and i am able to fetch the RetObjects
wrapping the list of Products perfectly fine at client.
这里我的问题是,在我的情况下,不是将我的产品列表包装到一个中间对象中,而是 RetObjects
,我无法直接编组和解组产品列表对象。如果我想编写另一个返回订单列表的服务,我需要用另一个中间对象包装它。实现这一目标的正确方法是什么?如果没有中间对象,我怎么能这样做?
Here my question is, instead of wrapping my List of Products into a intermediate object, RetObjects
in my case, couldn't I marshal and unmarshal List of Products object directly. If I want to write another service that returns List of Orders, I need to wrap this with one more intermediate object. What is the right approach to achieve this? How could I do this without intermediate objects?
推荐答案
首先
你不需要你自己的 MessageBodyWriter / Reader
。 Jersey / JAX-RS alread对此有标准支持。我会坚持使用默认值,除非你有一个真的,非常的理由需要你自己动手。
You don't need your own MessageBodyWriter/Reader
. Jersey/JAX-RS alread has standard support for this. I would stick with the default, unless you have a really, really good reason for needed to whip up your own.
第二
我们不需要包装,你可以简单回报 GenericEntity
。这将自动将元素包装在复数包装元素中,即< product>
- > < products>
。
We don't need the wrapper, you can simple return a GenericEntity
. This will automatically wrap the elements in a "plural wrapper" element, i.e. <product>
-> <products>
.
List<Product> list = new ArrayList<>();
GenericEntity<List<String>> entity = new GenericEntity<List<Product>>(list) {};
Response response = Response.ok(entity).build();
要接受资源方法中的正文,只需接受 List< Product>
作为参数就足够了。它将接受< products>< product />< product />< / products>
For accepting a body in resource method, simply accepting List<Product>
as an argument is enough. It will accept <products><product/><product/></products>
更新
检索列表< Product>
在客户端,我们应该使用 GenericType
。请这篇文章。
To retrieve the List<Product>
on the client side, we should make use of GenericType
. Se this post.
Jersey 1
WebResource resource = client.resource("...");
List<Product> products = resource.get(new GenericType<List<Product>>(){});
泽西岛2 / JAX-RS 2
Jersey 2/JAX-RS 2
Response response = ...
List<Product> products = response.readEntity(new GenericType<List<Product>>(){});
这篇关于Marshal / Un marshal使用JAXB在Jersey JAX-RS中列出对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!