Ok, im a happy camper..sort of. I got it working now. By using the JAXBElement<myclass> as a parameter in a post/put call, and the JAXBElement<myclass> as a response, I am able to see it go both ways now. The only thing I am not sure of is why I have to use JAXBElement<myclass> instead of myclass directly? I thought it handled both ways of doing things?
Regardless, this is kick ass stuff! XSD generated classes, being able to populate them and send them as xml or json, and handle xml/json requests/responses without doing any code to parse stuff.. directly manipulate the objects.. thats the way it should have been years ago! Hat's off to all you guys working on this stuff. Great job.
----- Original Message ----
From: Jakub Podlesak <Jakub.Podlesak_at_Sun.COM>
To: users_at_jersey.dev.java.net
Sent: Thursday, August 21, 2008 3:10:49 PM
Subject: Re: [Jersey] Pointers on using JAXB with Jersey
Hi Kevin,
On Thu, Aug 21, 2008 at 01:52:24PM -0700, Kevin Duffey wrote:
> Thanks Paul. I think I got the general idea.. but one thing I am not sure of.. since I have JAXB generated classes (they have the XMLElement, XMLType, XMLAccessorType defined in the generated source), and I include that .jar that contains those in the WEB-INF/lib of my REST app, do I need Provider classes as well, or does the Jersey/JAXB stuff know how to grab an instance of my generated class and return it as xml? As well, I am slightly confused on the ability for it to return xml or json... does it know how to do both for me already such that if the request Accept header says application/json, it returns that.
>
I presume your generated beans have also @XmlRootElement annotation on them.
Then it should work as you write, without a need to implement any providers.
You may try to download JsonFromJaxb example from [1], unzip it and run via
mvn compile exec:java
you should then be able to:
curl -i -HAccept:application/xml
http://localhost:9998/jsonfromjaxb/flights
HTTP/1.1 200 OK
server: grizzly/1.8.1
Content-Type: application/xml
Transfer-Encoding: chunked
Date: Thu, 21 Aug 2008 21:59:43 GMT
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><flights><flight><flightId>OK123</flightId><company>Czech Airlines</company><number>123</number><aircraft>B737</aircraft></flight><flight><flightId>OK124</flightId><company>Czech Airlines</company><number>124</number><aircraft>AB115</aircraft></flight></flights>
to get xml, and:
curl -i -HAcept:application/json
http://localhost:9998/jsonfromjaxb/flights
HTTP/1.1 200 OK
server: grizzly/1.8.1
Content-Type: application/json
Transfer-Encoding: chunked
Date: Thu, 21 Aug 2008 22:00:57 GMT
{"flight":[{"flightId":"OK123","company":"Czech Airlines","number":123,"aircraft":"B737"},{"flightId":"OK124","company":"Czech Airlines","number":124,"aircraft":"AB115"}]}
to get json.
The appropriate method is as simple as:
@GET
@Produces({"application/json", "application/xml"})
public synchronized Flights getFlightList() {
return myFlights;
}
Hope this helps,
~Jakub
[1]
http://download.java.net/maven/2/com/sun/jersey/samples/json-from-jaxb/0.9-ea-SNAPSHOT/json-from-jaxb-0.9-ea-SNAPSHOT-project.zip
> So I tried sending in a simple snippet of xml according to my xsd, but I haven't quite seen the method called yet. Getting various parser errors, like java.lang.IllegalArgumentException: java.text.ParseException: Expected separator '=' instead of '/'
> and the other one was MediaType not found for the request but I think I got that one worked out. I am using that Swing program called WizTools.org RESTclient 2.1.. so I select POST, give the url to my resource, and send in a snippet of xml. I have this feeling I am missing something small... just not sure what yet, but will keep pluggin away.
>
> Thanks.
>
>
>
>
>
>
>
> ----- Original Message ----
> From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
> To: users_at_jersey.dev.java.net
> Sent: Thursday, August 21, 2008 12:39:02 AM
> Subject: Re: [Jersey] Pointers on using JAXB with Jersey
>
> Hi Kevin,
>
> There is an example in the distribution for using JAXB with JSON.
>
> Just use the JAXB element directly:
>
> @POST
> @Consumes("application/xml")
> public void postJAXBBean(MyJAXBBean b) { ... }
>
>
>
> @GET
> @Produces("application/xml")
> public MyJAXBBean getJAXBBean() { ... }
>
>
> If you want JSON do this:
>
> @POST
> @Consumes({"application/xml", "application/json"})
> public void postJAXBBean(MyJAXBBean b) { ... }
>
>
>
> @GET
> @Produces({"application/xml", "application/json"})
> public MyJAXBBean getJAXBBean() { ... }
>
>
> It is important that MuJAXBBean is annotated with @XMLElement. If you want to use a JAXB type you can do:
>
> @POST
> @Consumes({"application/xml", "application/json"})
> public void postJAXBType(JAXBElement<MyJAXBType> b) { ... }
>
>
>
> @GET
> @Produces({"application/xml", "application/json"})
> public JAXBElement<MyJAXBType> getJAXBType() { ... }
>
>
> It is possible to configure the JSON output as well (search this list for JSONJAXBContext) as often the default mapping of JAXB beans to JSON can be too verbose or not quite right for JSON consumers.
>
> Paul.
>
> On Aug 21, 2008, at 2:30 AM, Kevin Duffey wrote:
>
> Hey all,
>
> Is there some good examples or links that explain a bit more how to make use of JAXB/schema generated objects to handle XML requests and return XML responses. I have a project generating my model classes using ant/Jaxb, and from my understanding there is a way to use resources and providers to automatically handle xml sent to resources so that I myself do not have to do any sort of parsing, I can instead access the generated object tree to deal with the request xml sent in, and as well, I can manipulate an instance of the model object tree and send it back as a xml response. It seems like a great way to handle xml anyway.
>
> Is there a JSON equivalent for handling json using objects and sending json back as a response.. preferably using the same JAXB generated object tree?
>
> Thank you.
>
>
--
http://blogs.sun.com/japod
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
For additional commands, e-mail: users-help_at_jersey.dev.java.net