开发者

JAXB Resteasy marshall desired fields

I would like to marshall an object using JAXB, the problems is that I would like to marshall just some fields depending on some business logic. For example I have an object like this:

Class Foo{
private name;
priv开发者_Go百科ate details;
private url;

//Getter and setter methods

}

What I want to achieve is send a JSON response with just the name and url field, like {name:"someName",url:"myUrl"}, so that when clients needs to retrieve more info they could request the uri sent in the URL field, and JAXB will marshall the details field this time. {name:"someName",url:"myUrl",details:"details"}.

I know I could use a wrapper class for that, but It's kindda annoying to write wrapper classes for the same pieces of data. So I was wondering if there's a way to configure JAXB to marshall just some fields. Thanks a lot.


By default JAXB will just marshal the properties that have values. This should give you the behaviour that you want.

Foo foo = new Foo();
foo.setName("Jane Doe");
marshaller.marshal(foo, System.out);  // results in <foo><name>Jane Doe</name></foo>
foo.setDetails("Some Details");
marshaller.marshal(foo, System.out);  // results in <foo><name>Jane Doe</name><details>Some Details</details></foo>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜