You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by "adam.galloway" <ad...@scala.com> on 2009/02/18 16:05:58 UTC

Another question about nillable=true and minOccurs=0

I have a JAX-RS service that Consumes json which maps to a POJO with JAXB. 
I'm having a hard time finding out how to tell wether a particular field has
been sent in with a null value or whether the value of the field was left
out of the incoming json.

The basic idea is that I have a pojo defined like this:

@XmlRootElement(name = "myPojo")
public class MyPojo {
   private Long id;
   private String name;
   private Date date;

   // getters and setters...
}

My service:

@Path("/myservice/")
public class MyService {

   @POST
   @Path("/mymethod/")
   @Consumes("application/json")
   public Response myMethod(MyPojo pojo) {
        //process pojo here
   }
}

What I'd like to be able to tell from MyService.myMethod() is whether
pojo.getDate() is null because the json had date nulled out or the json did
not include the date field.

Any suggestions would be greatly appreciated.
-- 
View this message in context: http://www.nabble.com/Another-question-about-nillable%3Dtrue-and-minOccurs%3D0-tp22080637p22080637.html
Sent from the cxf-user mailing list archive at Nabble.com.


Re: Another question about nillable=true and minOccurs=0

Posted by "henning.jensen" <he...@gmail.com>.

adam.galloway wrote:
> 
> What I'd like to be able to tell from MyService.myMethod() is whether
> pojo.getDate() is null because the json had date nulled out or the json
> did not include the date field.
> 

Since Java doesn't have nillable types in the same way that .NET do, I think
you would have a hard time solving this issue. With my experience of CXF and
nillable types I've seen that when defining an element nillable, but
required like this:

@XmlElement(nillable=true, required=true)
private Date date;

Then the consumer classes (generated by CXF) would be something like this: 

private JAXBElement<Date> date;

The JAXBElement object has a isNull-method that you can use to see if the
element was sent in as null:
<MyPojo>
  <date xsi:nil="true"/>
</MyPojo>

Or if the value was sent but had the value null inside:
<MyPojo>
  <date></date>
</MyPojo>

I'm not sure if it is possible to accomplish these two variants of null when
you expose a service, since CXF does all the mapping directly on your
regular classes (no stubs in the picture).

--
Henning
-- 
View this message in context: http://www.nabble.com/Another-question-about-nillable%3Dtrue-and-minOccurs%3D0-tp22080637p22089791.html
Sent from the cxf-user mailing list archive at Nabble.com.