You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by John Baker <jb...@dryfish.org.uk> on 2015/08/02 14:32:26 UTC
not fully serialising objects
Hello,
I'm using CXF 3.1.1 and am struggling with a REST service but am unsure
what exactly is wrong.
The service is defined via <jaxrs:service /> and it's working correctly
in so far as I can get various simple services to work, but one is
perplexing me.
Here is an example interface defining the service:
@Path("/myservice")
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public interface MyService {
@Path("/thing")
public Response query(@Valid MyThing thing);
}
And the client/test is using <jaxrs:client /> which injects client stub
into my test, ie
<jaxrs:client serviceClass="MyService" .. />
@Autowired
private MyService service;
The test calls the method, ie.
service.query(buildMyThing()):
and the CXF logging shows MyThing being serialised and sent to the
server side implementation. All fine so far.
However, if MyThing refers to another bean, and buildMyThing does this:
MyThing t= new MyThing();
t.setOtherThing(new OtherThing("a", 1, false));
Where the MyThing code looks like:
public class MyThing {
@NotNull
private OtherThing otherThing;
public void setOtherThing..
public OtherThing getOtherThing...
}
then the serialisation process, whether XML or JSON, serialises an empty
OtherThing excluding the properties within it. There are now two
outcomes:
1. When serialising as JSON, the client sends otherThing: {} and the
validation framework allows the request to succeed.
2. When serialising as XML, the client sends <otherThing /> and the
validation framework states that otherThing can not be null.
Neither outcome is what I expect to happen, ie OtherThing is fully
serialised. And one of the two above also seems wrong (at least, they
are not consistent).
Can anyone help me understand how to get OtherThing fully serialised?
Thanks
John
Re: not fully serialising objects
Posted by Sergey Beryozkin <sb...@gmail.com>.
On 02/08/15 14:27, John Baker wrote:
> I've made some progress. I needed to annotate methods in OtherThing with
> @XmlElement which I didn't realise was required.
>
> Also I've spotted some bizarre behaviour. The JSON and XML deserialiser
> do not quite work consistently if something like this is defined within
> an object:
>
> public List<Sort> getSorts() {
> ..
> }
>
> if an empty list is returned, the serialiser adds <sorts /> (XML) or
> sorts: [] (JSON), but the deserialiser calls:
>
>
> public void setSorts(List<Sort> sorts) {
> ..
> }
>
> it passes an empty list when deserialising JSON, and null if XML!
>
I quess JAXB defines how an empty list on the wire is injected into a
bean, i.e, as a null, while with the JSON to XML auto-conversion it is
an empty but initialized list. There might be some subtle differences in
a way Jetiison XMLStreamReader reports the events in such cases, as
opposed to a pure XML XMLStreamReader.
I'll keep this message and review when I get a chance to start working
on the next Jettison release
Sergey
>
> John
>
>
>
> On Sun, Aug 2, 2015, at 01:32 PM, John Baker wrote:
>> Hello,
>>
>> I'm using CXF 3.1.1 and am struggling with a REST service but am unsure
>> what exactly is wrong.
>>
>> The service is defined via <jaxrs:service /> and it's working correctly
>> in so far as I can get various simple services to work, but one is
>> perplexing me.
>>
>> Here is an example interface defining the service:
>>
>> @Path("/myservice")
>> @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
>> @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
>> public interface MyService {
>> @Path("/thing")
>> public Response query(@Valid MyThing thing);
>> }
>>
>> And the client/test is using <jaxrs:client /> which injects client stub
>> into my test, ie
>>
>> <jaxrs:client serviceClass="MyService" .. />
>>
>> @Autowired
>> private MyService service;
>>
>> The test calls the method, ie.
>>
>> service.query(buildMyThing()):
>>
>> and the CXF logging shows MyThing being serialised and sent to the
>> server side implementation. All fine so far.
>>
>> However, if MyThing refers to another bean, and buildMyThing does this:
>>
>> MyThing t= new MyThing();
>> t.setOtherThing(new OtherThing("a", 1, false));
>>
>> Where the MyThing code looks like:
>>
>> public class MyThing {
>> @NotNull
>> private OtherThing otherThing;
>>
>> public void setOtherThing..
>>
>> public OtherThing getOtherThing...
>> }
>>
>> then the serialisation process, whether XML or JSON, serialises an empty
>> OtherThing excluding the properties within it. There are now two
>> outcomes:
>>
>> 1. When serialising as JSON, the client sends otherThing: {} and the
>> validation framework allows the request to succeed.
>>
>> 2. When serialising as XML, the client sends <otherThing /> and the
>> validation framework states that otherThing can not be null.
>>
>> Neither outcome is what I expect to happen, ie OtherThing is fully
>> serialised. And one of the two above also seems wrong (at least, they
>> are not consistent).
>>
>> Can anyone help me understand how to get OtherThing fully serialised?
>>
>> Thanks
>>
>>
>> John
--
Sergey Beryozkin
Talend Community Coders
http://coders.talend.com/
Blog: http://sberyozkin.blogspot.com
Re: not fully serialising objects
Posted by John Baker <jb...@dryfish.org.uk>.
I've made some progress. I needed to annotate methods in OtherThing with
@XmlElement which I didn't realise was required.
Also I've spotted some bizarre behaviour. The JSON and XML deserialiser
do not quite work consistently if something like this is defined within
an object:
public List<Sort> getSorts() {
..
}
if an empty list is returned, the serialiser adds <sorts /> (XML) or
sorts: [] (JSON), but the deserialiser calls:
public void setSorts(List<Sort> sorts) {
..
}
it passes an empty list when deserialising JSON, and null if XML!
John
On Sun, Aug 2, 2015, at 01:32 PM, John Baker wrote:
> Hello,
>
> I'm using CXF 3.1.1 and am struggling with a REST service but am unsure
> what exactly is wrong.
>
> The service is defined via <jaxrs:service /> and it's working correctly
> in so far as I can get various simple services to work, but one is
> perplexing me.
>
> Here is an example interface defining the service:
>
> @Path("/myservice")
> @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
> @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
> public interface MyService {
> @Path("/thing")
> public Response query(@Valid MyThing thing);
> }
>
> And the client/test is using <jaxrs:client /> which injects client stub
> into my test, ie
>
> <jaxrs:client serviceClass="MyService" .. />
>
> @Autowired
> private MyService service;
>
> The test calls the method, ie.
>
> service.query(buildMyThing()):
>
> and the CXF logging shows MyThing being serialised and sent to the
> server side implementation. All fine so far.
>
> However, if MyThing refers to another bean, and buildMyThing does this:
>
> MyThing t= new MyThing();
> t.setOtherThing(new OtherThing("a", 1, false));
>
> Where the MyThing code looks like:
>
> public class MyThing {
> @NotNull
> private OtherThing otherThing;
>
> public void setOtherThing..
>
> public OtherThing getOtherThing...
> }
>
> then the serialisation process, whether XML or JSON, serialises an empty
> OtherThing excluding the properties within it. There are now two
> outcomes:
>
> 1. When serialising as JSON, the client sends otherThing: {} and the
> validation framework allows the request to succeed.
>
> 2. When serialising as XML, the client sends <otherThing /> and the
> validation framework states that otherThing can not be null.
>
> Neither outcome is what I expect to happen, ie OtherThing is fully
> serialised. And one of the two above also seems wrong (at least, they
> are not consistent).
>
> Can anyone help me understand how to get OtherThing fully serialised?
>
> Thanks
>
>
> John