FYI, I ended up creating a custom XMLFilter and use that in a Servlet filter to detect included/excluded fields on the query string in a dot notation (e.g. rootElement.childElement1). This notation is used like an xpath to the XML elements that will be filtered. So far working like a charm. I plan to extend this mechanism for JSON responses, but have not yet done so (should be simple enough).
If anyone is interested in the code, shoot me an email. I'd have to clean it up a tad bit to make it general use, but will do so if others have need. Might be useful if this or something similar made it's way into CXF base to save others some work. Cheers, David On Fri, Mar 6, 2009 at 6:44 PM, Daniel Kulp <[email protected]> wrote: > > Another option if you want to proceed down a different path would be to > write > a XMLStreamWriter that wrappers a different XMLStreamWriter and filters out > empty elements. Basically, track the writeStartElement/writeEndElement > calls > and anything with content and forward the appropriate calls into the > wrapped > writer. An interceptor could easily replace the existing writer with the > new > one. > > Dan > > > On Fri March 6 2009 6:30:16 pm David Castro wrote: > > Good to know about @XmlElement(required = false, nillable = false), > > although it doesn't appear to be causing the empty elements to be removed > > from the class I am mapping. Will keep fiddling though. > > > > Still wish there was an easy way to have CXF filter out fields on user > > request. I figured it would already exist in the framework, since it is > a > > fairly common requirement. Every tool can't do everything though =) > > > > Currently working on a way to manipulate the resultant XML document that > > CXF generates (through JAXB). I'll contribute it when I am done if > anyone > > is interested. > > > > Thanks for the help > > > > On Thu, Mar 5, 2009 at 6:18 PM, Daniel Kulp <[email protected]> wrote: > > > On Thu March 5 2009 8:42:50 pm Benson Margulies wrote: > > > > Isn't this for what minOccurs='0' was invented? In JAXB, I think > > > > that's required=false, nillable=true or some such. > > > > > > Right. It's actually required=false, nillable=false. With nillable > > > true, > > > if it's null, you get the element with xsi:nil="true" set. > > > > > > > > > Dan > > > > > > > On Thu, Mar 5, 2009 at 6:00 PM, David Castro <[email protected] > > > > > > > > wrote: > > > > > Hrmm, how about dynamically? Really what I am going for is > something > > > > > like: > > > > > > > > > > http://myapiserver.com/api/contacts?fields=id > > > > > > > > > > So this: > > > > > > > > > > <contact> > > > > > <id>0</id> > > > > > <firstName>A</firstName> > > > > > <lastName>B</lastName> > > > > > </contact> > > > > > > > > > > turns into this: > > > > > > > > > > <contact> > > > > > <id>0</id> > > > > > </contact> > > > > > > > > > > Although, in my particular case, I wouldn't just mind stripping out > > > > > all XML elements with empty content. I've already got a > > > > > filter/interceptor to empty fields I want to remove from the result > > > > > XML...but those tags still get included, which I want to avoid. > Want > > > > > to compress the XML to only fields that have data. > > > > > > > > > > Would still be great if there were simply a way of telling CXF on > the > > > > > > fly > > > > > > > > that only certain fields should make their way into the XML. If > you > > > > > > are > > > > > > > > aware of a better way...I'm all ears =) > > > > > > > > > > Thanks, > > > > > David > > > > > > > > > > On Thu, Mar 5, 2009 at 2:50 PM, Ian Roberts > > > > > > <[email protected]>wrote: > > > > >> David Castro wrote: > > > > >> > Is anyone is aware of how I can dynamically tell CXF (and thus > > > > >> > JAXB) that there are some fields I want excluded from the > > > > >> > resultant XML being > > > > >> > > > > >> returned? > > > > >> > > > > >> > I don't want to reinvent the wheel if I don't have to, but I > can't > > > > >> > find information on how to do what I want using the CXF > framework > > > > >> > rather than having to parse the XML outbound and remove what > needs > > > > >> > removing. Ideas? > > > > >> > > > > >> @XmlTransient tells JAXB to exclude particular fields. You might > > > > >> need to annotate either the field directly or its get/set methods, > > > > > > depending > > > > > > > >> on the @XmlAccessorType of the containing class. > > > > >> > > > > >> Ian > > > > >> > > > > >> -- > > > > >> Ian Roberts | Department of Computer Science > > > > >> [email protected] | University of Sheffield, UK > > > > > > -- > > > Daniel Kulp > > > [email protected] > > > http://www.dankulp.com/blog > > -- > Daniel Kulp > [email protected] > http://www.dankulp.com/blog >
