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>
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论