You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Mustafa Sezgin <ms...@aconex.com> on 2009/12/03 04:50:12 UTC

Marshalling null value fields

Hi all,

 

I have some beans in a rest service which marshalled and sent back to the
end user. However sometimes certain fields in the bean may not have values
and as a result, the XML that gets produced can sometimes omit these
fields. We want the responses to always be consistent, so even if a field
does not have a value, then an empty tag should be returned. Looking at
the XMLElement and XMLAttribute annotations, there doesn't seem to be
anything which helps with this. I don't want to have to go back and
manually set an empty string value to these fields. And some of these
fields are Date or Integer objects so I cant set an empty value to these
fields, like I can with a string. 

Any ideas on how to get around this problem would be appreciated..

 

Thanks

 


Re: Marshalling null value fields

Posted by Sergey Beryozkin <sb...@progress.com>.
Hi,

> After doing some investigation and prototyping, I have been thinking that
> if there is not an annotation based way of doing this, then writing an
> interceptor for the pre marshal phase and modifying the values of the
> object to be marshalled there. This requires I have all the fields in the
> object to be marshalled as strings. This just seems unnecessary, surely
> there has to be a better way of doing this...
>
> Also, to get the object that will be marshalled from the Message object,
> it seems I have to do :
>
> public void handleMessage(Message message) throws Fault {
>        List<Object> responseObjects = message.getContent(List.class);
>        Object responseObject = responseObjects.get(0);
>
> }

This is one way to update the existing object, before it's being serialized,  but a simpler option might be to register a custom 
ResponseHandler filter. It will be called just before the seriialization occurs, and the object can be retrieved from the in 
Response.getEntity().

Another option is to register a custom XMLStreamWriter, by either extending the existing JAXBElementProvider or doing it from the 
custom ResponseHandler filter, please see :

http://cwiki.apache.org/CXF20DOC/jax-rs.html#JAX-RS-CustomizingrequestandresponseXML

this might be of interest too :

http://cwiki.apache.org/CXF20DOC/jax-rs.html#JAX-RS-CustomizingJAXBandJSONinputandoutput

actually, those 2 section will need to be merged...

hope it helps, Sergey



>
> Is this correct or is there an easier way of doing this?
> Will the responseObjects list I get back always contain one element?
>
> -----Original Message-----
> From: Mustafa Sezgin [mailto:msezgin@aconex.com]
> Sent: Thursday, 3 December 2009 2:50 PM
> To: users@cxf.apache.org
> Subject: Marshalling null value fields
>
> Hi all,
>
>
>
> I have some beans in a rest service which marshalled and sent back to the
> end user. However sometimes certain fields in the bean may not have values
> and as a result, the XML that gets produced can sometimes omit these
> fields. We want the responses to always be consistent, so even if a field
> does not have a value, then an empty tag should be returned. Looking at
> the XMLElement and XMLAttribute annotations, there doesn't seem to be
> anything which helps with this. I don't want to have to go back and
> manually set an empty string value to these fields. And some of these
> fields are Date or Integer objects so I cant set an empty value to these
> fields, like I can with a string.
>
> Any ideas on how to get around this problem would be appreciated..
>
>
>
> Thanks
>
>
> 


RE: Marshalling null value fields

Posted by Mustafa Sezgin <ms...@aconex.com>.
After doing some investigation and prototyping, I have been thinking that
if there is not an annotation based way of doing this, then writing an
interceptor for the pre marshal phase and modifying the values of the
object to be marshalled there. This requires I have all the fields in the
object to be marshalled as strings. This just seems unnecessary, surely
there has to be a better way of doing this...

Also, to get the object that will be marshalled from the Message object,
it seems I have to do :

public void handleMessage(Message message) throws Fault {
        List<Object> responseObjects = message.getContent(List.class);
        Object responseObject = responseObjects.get(0);
	  
}

Is this correct or is there an easier way of doing this?
Will the responseObjects list I get back always contain one element?

-----Original Message-----
From: Mustafa Sezgin [mailto:msezgin@aconex.com] 
Sent: Thursday, 3 December 2009 2:50 PM
To: users@cxf.apache.org
Subject: Marshalling null value fields

Hi all,

 

I have some beans in a rest service which marshalled and sent back to the
end user. However sometimes certain fields in the bean may not have values
and as a result, the XML that gets produced can sometimes omit these
fields. We want the responses to always be consistent, so even if a field
does not have a value, then an empty tag should be returned. Looking at
the XMLElement and XMLAttribute annotations, there doesn't seem to be
anything which helps with this. I don't want to have to go back and
manually set an empty string value to these fields. And some of these
fields are Date or Integer objects so I cant set an empty value to these
fields, like I can with a string. 

Any ideas on how to get around this problem would be appreciated..

 

Thanks

 


Re: Marshalling null value fields

Posted by Daniel Kulp <dk...@apache.org>.

Normally, for the XMLElement, you would add the nillable=true attribute to 
@XmlElement.   That would write out the element with a xsi:nil=true attribute.

Dan


On Wed December 2 2009 10:50:12 pm Mustafa Sezgin wrote:
> Hi all,
> 
> 
> 
> I have some beans in a rest service which marshalled and sent back to the
> end user. However sometimes certain fields in the bean may not have values
> and as a result, the XML that gets produced can sometimes omit these
> fields. We want the responses to always be consistent, so even if a field
> does not have a value, then an empty tag should be returned. Looking at
> the XMLElement and XMLAttribute annotations, there doesn't seem to be
> anything which helps with this. I don't want to have to go back and
> manually set an empty string value to these fields. And some of these
> fields are Date or Integer objects so I cant set an empty value to these
> fields, like I can with a string.
> 
> Any ideas on how to get around this problem would be appreciated..
> 
> 
> 
> Thanks
> 

-- 
Daniel Kulp
dkulp@apache.org
http://www.dankulp.com/blog