users@jersey.java.net

Re: consume xml

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Wed, 23 Jan 2008 14:14:34 +0100

guilhem legal wrote:
> I'm using both tomcat 6 and tomcat 5.5, but we are planning to pass on
> glassfish ( we want but our client are not really ready to change).
>

OK.


> So if i have understand i have to pass from this web.xml
> <servlet>
> <servlet-name>ServletAdaptor</servlet-name>
>
> <servlet-class>com.sun.ws.rest.impl.container.servlet.ServletAdaptor</servlet-class>
>
> <init-param>
> <param-name>webresourceclass</param-name>
> <param-value>webresources.WebResources</param-value> <--
> where my webResource.java was
> </init-param>
> <load-on-startup>1</load-on-startup>
> </servlet>
> <servlet-mapping>
> <servlet-name>ServletAdaptor</servlet-name>
> <url-pattern>/WS/*</url-pattern>
> </servlet-mapping>
>
> to this :
>

Yes (but there is a simpler alternative for Tomcat, see end of email):


> <servlet>
> <servlet-name>ServletAdaptor</servlet-name>
>
> <servlet-class>com.sun.ws.rest.impl.container.servlet.ServletAdaptor</servlet-class>
>
> <init-param>
>
> <param-name>com.sun.ws.rest.config.property.resourceConfigClass</param-name>
>
>
> <param-value>com.sun.ws.rest.api.core.PackagesResourceConfig</param-value>
> </init-param>
> <init-param>
>
> <param-name>com.sun.ws.rest.config.property.packages</param-name>
> <param-value>folder of my
> resource</param-value> <--
> where my classes with @Path are

Not a folder but a list of one or more package names separated by ';'.

For example, if you have resource classes in packages:

   com.ache.resources.foo
   com.ache.resources.bar

the value could be:

   com.ache.resources.foo;com.ache.resources.bar

but you could just just use the package:

    com.ache.resources

as the scanner will search sub-packages, this is nice if you add another
resource say in:

   com.ache.resources.baz

as you don't have to change the configuration.


> </init-param>
> <load-on-startup>1</load-on-startup>
> </servlet>
> <servlet-mapping>
> <servlet-name>ServletAdaptor</servlet-name>
> <url-pattern>/WS/*</url-pattern>
> </servlet-mapping>
>

An alternative is this:

<servlet>
        <servlet-name>ServletAdaptor</servlet-name>
<servlet-class>com.sun.ws.rest.impl.container.servlet.ServletAdaptor</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>ServletAdaptor</servlet-name>
            <url-pattern>/WS/*</url-pattern>
        </servlet-mapping>
</servlet>

This requires no additional configuration but is less portable (it works
on Tomcat and GF) than using the package configuration. By default it
will scan the "WEB-INF/lib" and "WEB-INF/classes" for root resource Java
class files. Great for getting started.

Note that all this is documented in the JavaDoc of:

   com.sun.ws.rest.spi.container.servlet.ServletContainer

(ServletAdaptor inherits from the above class).

Paul.

> Guilhem Legal
> Paul Sandoz a écrit :
>> Hi Guilhem,
>>
>> You can delete this code and all other code generated when using 0.4.
>> The build process is now simplified and there no required pre-compile
>> step that generates code for WADL or the set of root resource classes.
>> Standard build configurations will work.
>>
>> WADL is now generated dynamically from the same URL and for each
>> resource (if you have JAXB stuff in the classpath, if you resources
>> will still work but WADL will not be produced) .
>>
>> By default it is no longer necessary to configure anything specific in
>> the web.xml if using Glassfish, Tomcat or Jetty (in standard war
>> deployment mode). See my blog [1] for a description of this. What web
>> container are you using?
>>
>> Paul.
>>
>> [1] http://blogs.sun.com/sandoz/entry/jersey_0_5_is_released
>>
>> guilhem legal wrote:
>>> I'm passing to jersey 0.5 and i don't what to do in my
>>> WadlResource.java .
>>> This file was originally build by netbeans before i pass on a maven
>>> build.
>>>
>>>
>>> package com.sun.ws.rest.wadl.resource;
>>>
>>> import java.io.InputStream;
>>> import javax.ws.rs.ProduceMime;
>>> import javax.ws.rs.Path;
>>> import javax.ws.rs.core.HttpContext;
>>> import javax.ws.rs.core.UriInfo;
>>> import com.sun.ws.rest.impl.wadl.WadlReader;
>>> import javax.ws.rs.GET;
>>>
>>>
>>> /**
>>> * This class was generated.
>>> *
>>> * It is used to retrieve a WADL description
>>> * of all of the other resources
>>> *
>>> *
>>> */
>>> @ProduceMime("application/vnd.sun.wadl+xml")
>>> @Path("/application.wadl")
>>> public class WadlResource {
>>>
>>> @HttpContext
>>> public UriInfo uriInfo;
>>>
>>> @GET
>>> public String getWadl() {
>>> InputStream is =
>>> this.getClass().getResourceAsStream("application.wadl");
>>> String str = WadlReader.read(is, uriInfo.getBaseUri());
>>> return str;
>>> }
>>>
>>> }
>>>
>>> WadlReader is no longer available so what i'm supose to do?
>>>
>>> Guilhem Legal
>>>
>>> Paul Sandoz a écrit :
>>>> 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 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