users@jersey.java.net

Re: consume xml

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Wed, 23 Jan 2008 11:06:46 +0100

Hi Guilhem,

guilhem legal wrote:
> No I don't get an exception on the server side, it looks like i never
> received the request.

It has been a long time since i used 0.4 :-) so i am guessing it is not
printing out the error messages or there is a bug.


> And yes I want to handle multiple JAXB type. I m using jersey 0.4, maybe
> I will pass to 0.5 to see if there is any change.

I verified you will get a meaningful error message for 0.5 but
unfortunately multiple JAXB types are currently not supported when using
Object. In any case if you need any help transitioning let me know. The
changes.txt will help guide you [1].

There are two issues with the current JAXB message body reader:

1) it only supports types that are annotated with @XmlRootElement, in
    this case the type is Object; and

2) it has know way of determining what JAXBContext to use.

For the 0.6 release we will be working on a solution for 2) to allow an
application to define the set of JAXB contexts to use for Java types.
But a solution for 1) may be a little more tricky.


> Maybe a solution is that :
>
> @HttpMethod("POST")
> @ConsumeMime("text/xml")
> public Response handleObject1Xml(Object1 request) throws JAXBException {
> ----------------------------------------------------------------
> }
>
> @HttpMethod("POST")
> @ConsumeMime("text/xml")
> public Response handleObject2Xml(Object2 request) throws JAXBException {
> ----------------------------------------------------------------
> }
> it not really what i want but is this working?
>

Unfortunately not because Jersey makes the decision on what Java method
to choose based on the annotations and HTTP request headers, and not on
the content of the message. So the above example is ambiguous and is
actually a validation issue, at least a warning should be produced (we
are working to improve that).

For now I recommend you implement it this way:

    @POST
    @ConsumeMime("text/xml")
    public Response post(InputStream is) throws JAXBException {
       Unmarshaller u = context.createUnmarshaller();
       Object o = null;
       try {
          o = u.unmarshal(is);
       } catch (UnmarshalException e) {
          throw new WebApplicationException(400);
       }
       ...
    }

Note the throwing of WebApplicationException with a 400 (Client Error)
for the case when the client sends incorrect XML that cannot be
unmarshalled using the JAXB context.


I have logged an issue [2] to track this.

Hope this helps,
Paul.

[1]
https://jersey.dev.java.net/source/browse/*checkout*/jersey/tags/jersey-0.5/jersey/changes.txt?rev=697
[2] https://jersey.dev.java.net/issues/show_bug.cgi?id=34

> Guilhem Legal
>
> Paul Sandoz a écrit :
>> guilhem legal wrote:
>>> hi!
>>>
>>> I'm trying to do a simple thing (I think) with jersey but its not
>>> working.
>>> I want to handle two different POST request with diferent MIME type.
>>> I use an other application whitch is supposed to send the request :
>>>
>>
>> Are you getting an exception on the server side when the client POSTs
>> the request with the content type of "text/xml" ?
>>
>> I just wrote a little example and verified that i get an exception in
>> this case, with the error:
>>
>> Caused by: java.lang.IllegalArgumentException: A message body reader
>> for Java type, class java.lang.Object, and MIME media type, text/xml,
>> was not found
>>
>> Note i am using Jersey 0.6 (but for this case it is equivalent to that
>> in 0.5).
>>
>> I think the problem is you are using the Object type for the request
>> entity of the doPOSTXml method. The runtime does not know what to do
>> with Object type. If you use an explicit JAXB type then it should work.
>>
>> But maybe you want to handle multiple JAXB types for the request
>> entity? Unfortunately that is not currently possible. I am not sure it
>> is possible given the current way message body readers work, i would
>> need to look more closely at this.
>>
>> Paul.
>>
>>> my code :
>>>
>>> @HttpMethod("GET")
>>> public Response doGET() throws JAXBException {
>>>
>>> return treatIncommingRequest();
>>> }
>>> @HttpMethod("POST")
>>> @ConsumeMime("application/x-www-form-urlencoded")
>>> public Response doPOSTKvp(String request) throws JAXBException {
>>> -------------------------------------------------------
>>> }
>>> @HttpMethod("POST")
>>> @ConsumeMime("text/xml")
>>> public Response doPOSTXml(Object request) throws JAXBException {
>>> ----------------------------------------------------------------
>>> }
>>>
>>> It never enter in the method doPOSTXml. So have i make a mistake?
>>>
>>> Guilhem Legal
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>

-- 
| ? + ? = To question
----------------\
    Paul Sandoz
         x38109
+33-4-76188109