You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Doug <Do...@gmail.com> on 2008/03/18 02:33:51 UTC

JSR-311 and JAXB and JSON

Hi,
I am trying to set up a JSR-311 REST "like" service and POSTing a string 
of JSON  to my /service/init/{id} address. The server seems to recognise 
the address because catalina.out reports:

Mar 18, 2008 11:11:37 AM org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor 
handleMessage
INFO: Found operation: init

The problem is that my init service function is never actually called. I 
figure that something is going wrong with the JAXB? interpretation of my JSON
data. Is there some way to switch on debugging of this process to see 
what is or isn't going on?
After the above server message my client connection eventually 
just times out.

I am just assuming that consuming application/json works ...?
	@POST
	@Path("init/{clientId}")
	@ProduceMime("text/plain")
	@ConsumeMime("application/json")
	public String init(@UriParam("clientId") String id, @HttpContext UriInfo 
info, CrystalParameters cp) {
		System.out.println("-----------received the sucker!----------");
		MultivaluedMap params  = info.getQueryParameters();
		// nothing happening here yet ...
		
		String url = "/ReflnCover/" + id + "/basic";
		return url;
	}

Thanks
Doug

P.S. I'm using apache-cxf-2.1-incubator-20080306.021818-37.zip under 
apache-tomcat-5.5.20 on kubuntu.

Re: JSR-311 and JAXB and JSON

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

This really looks like a Jettison issue.   It might be good to create a 
simple main() method program the just serializes your   
CrystalParameters to the Jettison stream and attach that to a JIRA at 
the jettison bug tracker:
http://jira.codehaus.org/browse/JETTISON

That said, as Sergey mentioned, you MIGHT be able to work around it in 
this specific case by putting an annotation like:

@XmlType(name = "", propOrder = {
    "wavelength",
    "bottomOffset",
    "upperOffset",
    "leftOffest",
    "rightOffset",
    "d2x"
})

To put the d2x stuff at the very end.  

Dan


On Wednesday 19 March 2008, Doug wrote:
> Actually there is something funny going wrong with the JSON
> emitted by the JAXRS process. This was just a dummy service to
> see the output format:
>
> @GET
> @Path("get/{clientId}")
> @ProduceMime("application/json")
> public CrystalParameters dummy(@UriParam("clientId") String id,
> @HttpContext UriInfo info) {
> 	CrystalParameters  cpn = new CrystalParameters();
> 	MachineData md = new MachineData();
> 	md.setWavelength(0.7);
> 	md.setD2x(new double[] {1.0,0,0,0,1.0,0,0,0,1.});
> 	md.setBottomOffset(50.);md.setUpperOffset(50.);
> 	md.setLeftOffset(50.); md.setRightOffset(50.);
> 	cpn.setMachineData(md);
> 	return cpn;
> }
>
> When I did a wget on this service it gave me JSON:
> {"CrystalParameters":{"machineData":{"bottomOffset":50,"d2x":
> [1,0,0,0,1,0,0,0,1,50,50,50,0.7]}}}
>
> the XML precursor was, from my previous email:
>
> <machineData><bottomOffset>50.0</bottomOffset>
>   
> <d2x>1.0</d2x><d2x>0.0</d2x><d2x>0.0</d2x><d2x>0.0</d2x><d2x>1.0</d2x>
> <d2x>0.0</d2x><d2x>0.0</d2x><d2x>0.0</d2x><d2x>1.0</d2x>
>    <leftOffset>50.0</leftOffset><rightOffset>50.0</rightOffset>
>    <upperOffset>50.0</upperOffset><wavelength>0.7</wavelength>
>  </machineData>
>
> i.e., the d2x array soaked up all trailing MachineData bean parameters
> in the emitted JSON representation.
>
> So unless there is some JAXB annotation stuff for arrays in java
> beans, I'm guessing both the in and out JSON to XML JAXRS convertors
> may be broken?
>
> Thanks for any info.
> Doug



-- 
J. Daniel Kulp
Principal Engineer, IONA
dkulp@apache.org
http://www.dankulp.com/blog

Re: JSR-311 and JAXB and JSON

Posted by Sergey Beryozkin <se...@iona.com>.
Hi Doug

> So unless there is some JAXB annotation stuff for arrays in java beans,
> I'm guessing both the in and out JSON to XML JAXRS convertors may be broken?

This is a grey area for me. Default JSON converter does not do anything but delegating to a Jettison library. Likewise, I'm not sure 
if there's some JAXB annotation which can be used to annotate arrays to give some hint to a JAXB (de)serializer...

Does anyone know what might be happening here ?

> i.e., the d2x array soaked up all trailing MachineData bean parameters
> in the emitted JSON representation.

I'm just wondering, would it work if a d2x array were placed as the last entry in the CrystalParams's class/schema, as a workaround 
?

Cheers, Sergey


> Actually there is something funny going wrong with the JSON
> emitted by the JAXRS process. This was just a dummy service to
> see the output format:
>
> @GET
> @Path("get/{clientId}")
> @ProduceMime("application/json")
> public CrystalParameters dummy(@UriParam("clientId") String id, @HttpContext
> UriInfo info) {
> CrystalParameters  cpn = new CrystalParameters();
> MachineData md = new MachineData();
> md.setWavelength(0.7);
> md.setD2x(new double[] {1.0,0,0,0,1.0,0,0,0,1.});
> md.setBottomOffset(50.);md.setUpperOffset(50.);
> md.setLeftOffset(50.); md.setRightOffset(50.);
> cpn.setMachineData(md);
> return cpn;
> }
>
> When I did a wget on this service it gave me JSON:
> {"CrystalParameters":{"machineData":{"bottomOffset":50,"d2x":
> [1,0,0,0,1,0,0,0,1,50,50,50,0.7]}}}
>
> the XML precursor was, from my previous email:
>
> <machineData><bottomOffset>50.0</bottomOffset>
>   <d2x>1.0</d2x><d2x>0.0</d2x><d2x>0.0</d2x><d2x>0.0</d2x><d2x>1.0</d2x>
>   <d2x>0.0</d2x><d2x>0.0</d2x><d2x>0.0</d2x><d2x>1.0</d2x>
>   <leftOffset>50.0</leftOffset><rightOffset>50.0</rightOffset>
>   <upperOffset>50.0</upperOffset><wavelength>0.7</wavelength>
> </machineData>
>
> i.e., the d2x array soaked up all trailing MachineData bean parameters
> in the emitted JSON representation.
>
> So unless there is some JAXB annotation stuff for arrays in java beans,
> I'm guessing both the in and out JSON to XML JAXRS convertors may be broken?
>
> Thanks for any info.
> Doug 

----------------------------
IONA Technologies PLC (registered in Ireland)
Registered Number: 171387
Registered Address: The IONA Building, Shelbourne Road, Dublin 4, Ireland

Re: JSR-311 and JAXB and JSON

Posted by Doug <Do...@gmail.com>.
Actually there is something funny going wrong with the JSON
emitted by the JAXRS process. This was just a dummy service to 
see the output format:

@GET
@Path("get/{clientId}")
@ProduceMime("application/json")
public CrystalParameters dummy(@UriParam("clientId") String id, @HttpContext 
UriInfo info) {
	CrystalParameters  cpn = new CrystalParameters();
	MachineData md = new MachineData();
	md.setWavelength(0.7);
	md.setD2x(new double[] {1.0,0,0,0,1.0,0,0,0,1.});
	md.setBottomOffset(50.);md.setUpperOffset(50.);
	md.setLeftOffset(50.); md.setRightOffset(50.);
	cpn.setMachineData(md);
	return cpn;
}

When I did a wget on this service it gave me JSON:
{"CrystalParameters":{"machineData":{"bottomOffset":50,"d2x":
[1,0,0,0,1,0,0,0,1,50,50,50,0.7]}}}

the XML precursor was, from my previous email:

<machineData><bottomOffset>50.0</bottomOffset>
   <d2x>1.0</d2x><d2x>0.0</d2x><d2x>0.0</d2x><d2x>0.0</d2x><d2x>1.0</d2x>
   <d2x>0.0</d2x><d2x>0.0</d2x><d2x>0.0</d2x><d2x>1.0</d2x>
   <leftOffset>50.0</leftOffset><rightOffset>50.0</rightOffset>
   <upperOffset>50.0</upperOffset><wavelength>0.7</wavelength>
 </machineData>

i.e., the d2x array soaked up all trailing MachineData bean parameters 
in the emitted JSON representation.

So unless there is some JAXB annotation stuff for arrays in java beans, 
I'm guessing both the in and out JSON to XML JAXRS convertors may be broken?

Thanks for any info.
Doug

Re: JSR-311 and JAXB and JSON

Posted by Doug <Do...@gmail.com>.
Thanks Sergey,
I think it is a problem with the format of my JSON.
I can actually get my service to respond by
POSTing the following equivalent XML:

<?xml version='1.0' encoding='UTF-8'?>
<CrystalParameters>
 <cellData><a>5.0</a><alpha>5.0</alpha><b>5.0</b>
         <beta>5.0</beta><c>5.0</c><gamma>5.0</gamma>
 </cellData>
 <machineData><bottomOffset>50.0</bottomOffset>
   <d2x>1.0</d2x><d2x>0.0</d2x><d2x>0.0</d2x><d2x>0.0</d2x><d2x>1.0</d2x>
   <d2x>0.0</d2x><d2x>0.0</d2x><d2x>0.0</d2x><d2x>1.0</d2x>
   <leftOffset>50.0</leftOffset><rightOffset>50.0</rightOffset>
   <upperOffset>50.0</upperOffset><wavelength>0.7</wavelength>
 </machineData>
 <scanData><chi>54.74</chi><detector>50.2</detector><direction>-</direction>
      <kappa>0.0</kappa><omega>-15.0</omega><phi>0.0</phi><scan>Omega</scan>
     <sweep>130.0</sweep><twotheta>-28.8</twotheta><width>5.0</width>
 </scanData>
 <scanData><chi>54.74</chi><detector>50.2</detector><direction>-</direction>
     <kappa>0.0</kappa><omega>-15.0</omega><phi>120.0</phi><scan>Omega</scan>
    <sweep>130.0</sweep><twotheta>-28.8</twotheta><width>5.0</width>
 </scanData>
</CrystalParameters>

but POSTing this JSON string doesn't work:

{"CrystalParameters":{"machineData":{"wavelength":0.71069,
 "d2x":[1,0,0,0,1,0,0,0,1],
 "leftOffset":0.05,"rightOffset":0.05,"upperOffset":0.05,"bottomOffset":0.05},
"scanData":[{"scan":"Omega","detector":50.2,"twotheta":-28.8,"omega":-15,
  "phi":0,"chi":54.74,"width":5,"sweep":130,"direction":"-"},  
       {"scan":"Omega","detector":50.2,"twotheta":-28.8,"omega":-15,
   "phi":120,"chi":54.74,"width":5,"sweep":130,"direction":"-"}],
"crystalData":{"a":5,"b":5,"c":5,"alpha":90,"beta":90,"gamma":90}}}

Actually if I strip it down to just:

{"CrystalParameters":{"crystalData":
{"a":5,"b":5,"c":5,"alpha":90,"beta":90,"gamma":90}}} 

then the service calls init correctly, but if I try to include any kind
of array data in the JSON string then the init 
just never gets called, e.g.

{"CrystalParameters":{"crystalData":
{"a":5,"b":5,"c":5,"alpha":90,"beta":90,"gamma":90,
"ub"=[0.2,0,0,0,0.2,0,0,0,0.2]}}}

Do arrays need some special treatment? ub, like d2x above is 
expected to be just java type double[].

Thanks
Doug

Re: JSR-311 and JAXB and JSON

Posted by Sergey Beryozkin <se...@iona.com>.
Hi Doug


There's a system test verifying that a json text can be converted into a JAXB-annotated
Book class, please see systests/jaxrs/JAXRSClientServerBookTest.updateBookWithJSON();

Jettison 1.0 (MappedXMLInputStream) is used as a message body reader for JSON data, can it be that it can not handle the incoming 
JSON stream ? I know that after Dan K has upgraded the version of Jettison he had to update the test Json fragments too...
Another possibility is that  CrystalParameters is not having an @XMLRootElement annotation ?

Perhaps a BadgerFishProvider can do better ? Please see a systest/jaxrs/JAXRS...SpringProviderTest

Cheers, Sergey

> I am just assuming that consuming application/json works ...?
> @POST
> @Path("init/{clientId}")
> @ProduceMime("text/plain")
> @ConsumeMime("application/json")
> public String init(@UriParam("clientId") String id, @HttpContext UriInfo
> info, CrystalParameters cp) {
> System.out.println("-----------received the sucker!----------");
> MultivaluedMap params  = info.getQueryParameters();
> // nothing happening here yet ...
>
> String url = "/ReflnCover/" + id + "/basic";
> return url;
> }
>
> Thanks
> Doug
>
> P.S. I'm using apache-cxf-2.1-incubator-20080306.021818-37.zip under
> apache-tomcat-5.5.20 on kubuntu. 

----------------------------
IONA Technologies PLC (registered in Ireland)
Registered Number: 171387
Registered Address: The IONA Building, Shelbourne Road, Dublin 4, Ireland