users@jersey.java.net

Re: How to pass objects in as a method parameter?

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 15 Apr 2008 19:36:34 +0200

Hi Travis,

Travis Reeder wrote:
> Ok, thanks guys. Do you see a good use for this type of functionality
> though?

I think it could (this has been raised before by others). If we develop
a bean-based approach to forms (we have all the pieces to do that), then
the parameter approach is essentially an unwrapping of the latter (and
there are ways to plug in different HTTP method processing rules in
Jersey). Time is the main factor... :-)

IMHO I think there needs to be a clearer way to distinguish POST methods
that process forms from POST methods that process other types of
entities. For example,

@FormPOST
public String tester(String x, @Format("application/xml") Profile p) {
}

It also may be possible to unify form-based processing using the URL
encoded mechanism or multipart MIME.

Paul.

> Maybe it's something that should be added as I think it's
> pretty powerful to be able to pass multiple objects in as parameters.
>
> Maybe annotated like:
>
> @POST
> @Path("/test")
> public String tester(String x, @Format("application/xml") Profile p){
> System.out.println("x=" + x);
> System.out.println("profile=" + p);
> return "allgood";
> }
>
> I'm sure there's a better annotation for it, but you get the idea.
>
>
> On Tue, Apr 15, 2008 at 2:21 AM, Jakub Podlesak <Jakub.Podlesak_at_sun.com
> <mailto:Jakub.Podlesak_at_sun.com>> wrote:
>
>
> Hi Travis,
>
> you can send only one entity in one request.
> It means:
> i) if x is part of the profile, it should be included in p
> ii) if x is something else, you should probably introduce a new resource
> for it
> iii) if you want client to control resulted URI of the created resource,
> and x is part of the URI
> you could use PUT method instead and x would be a path param
>
> i.e. something like:
>
> @PUT @Path("/profiles/{id}") @ConsumeMime("application/xml")
> public void putProfile(@PathParam("id") String x, Profile p) {
> // do whatever you want with p, x
> }
>
> Hope it helps,
>
> ~Jakub
>
> On Mon, Apr 14, 2008 at 06:44:57PM -0700, Travis Reeder wrote:
> > So here's the method I'm testing:
> >
> > @POST
> > @Path("/test")
> > public String tester(String x, Profile p){
> > System.out.println("x=" + x);
> > System.out.println("profile=" + p);
> > return "allgood";
> > }
> >
> > Here's the test code:
> >
> > HttpClient client = new HttpClient();
> > PostMethod post = new PostMethod("
> > http://localhost:8081/rest/profiles/test");
> > Profile pro = new Profile();
> > pro.setFirstName("Scooby");
> > pro.setLastName("Doo");
> > JAXBContext jaxbContext =
> JAXBContext.newInstance(Profile.class);
> > Marshaller marshaller = jaxbContext.createMarshaller();
> > StringWriter writer = new StringWriter();
> > marshaller.marshal(pro, writer);
> > post.addParameter("p", writer.toString());
> > post.addParameter("x", "sooby_at_doo.com
> <mailto:sooby_at_doo.com>");
> > System.out.println(post.getParameter("p"));
> > int status = client.executeMethod(post);
> > System.out.println("status=" + status);
> > String response = post.getResponseBodyAsString();
> > System.out.println("response=" + response);
> >
> > The Profile class has @XmlRootElement and marshals/unmarshals
> fine when
> > using JAXB alone and when returning it via an @Get method. But
> when I try to
> > run it with the method above, I get:
> >
> > Caused by: com.sun.ws.rest.api.container.ContainerException:
> Exception
> > injecting parameters to Web resource method
> > at
> >
> com.sun.ws.rest.impl.model.method.dispatch.EntityParamDispatchProvider$EntityParamInInvoker.getParams(EntityParamDispatchProvider.java:79)
> > at
> >
> com.sun.ws.rest.impl.model.method.dispatch.EntityParamDispatchProvider$TypeOutInvoker._dispatch(EntityParamDispatchProvider.java:105)
> > at
> >
> com.sun.ws.rest.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:66)
> > at
> >
> com.sun.ws.rest.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:108)
> > at
> >
> com.sun.ws.rest.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:88)
> > at
> >
> com.sun.ws.rest.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:55)
> > at
> >
> com.sun.ws.rest.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:88)
> > at
> >
> com.sun.ws.rest.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:49)
> > at
> >
> com.sun.ws.rest.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:369)
> > at
> >
> com.sun.ws.rest.spi.container.servlet.ServletContainer.service(ServletContainer.java:130)
> > ... 19 more
> > Caused by: java.lang.IllegalArgumentException:
> java.io.IOException: Error
> > marshalling out JAXB object of type "class a.b.c.Profile".
> > at
> >
> com.sun.ws.rest.spi.container.AbstractContainerRequest.getEntity(AbstractContainerRequest.java:204)
> > at
> >
> com.sun.ws.rest.impl.model.method.dispatch.EntityParamDispatchProvider$EntityExtractor.extract(EntityParamDispatchProvider.java:57)
> > at
> >
> com.sun.ws.rest.impl.model.method.dispatch.EntityParamDispatchProvider$EntityParamInInvoker.getParams(EntityParamDispatchProvider.java:73)
> > ... 28 more
> > Caused by: java.io.IOException: Error marshalling out JAXB object
> of type
> > "class a.b.c.Profile".
> > at
> >
> com.sun.ws.rest.impl.provider.entity.XMLJAXBElementProvider.readFrom(XMLJAXBElementProvider.java:50)
> > at
> >
> com.sun.ws.rest.spi.container.AbstractContainerRequest.getEntity(AbstractContainerRequest.java:201)
> > ... 30 more
> > Caused by: javax.xml.bind.UnmarshalException
> > - with linked exception:
> > [org.xml.sax.SAXParseException: Premature end of file.]
> > at
> >
> javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:315)
> > at
> >
> com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:481)
> > at
> >
> com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:199)
> > at
> >
> com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:168)
> > at
> >
> javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)
> > at
> >
> javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:184)
> > at
> >
> com.sun.ws.rest.impl.provider.entity.XMLJAXBElementProvider.readFrom(XMLJAXBElementProvider.java:48)
> > ... 31 more
> > Caused by: org.xml.sax.SAXParseException: Premature end of file.
> > at
> >
> com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
> > at
> >
> com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:174)
> > at
> >
> com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:388)
> > at
> >
> com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1411)
> > at
> >
> com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:1044)
> > at
> >
> com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:647)
> > at
> >
> com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
> > at
> >
> com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:508)
> > at
> >
> com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:807)
> > at
> >
> com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
> > at
> >
> com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:107)
> > at
> >
> com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
> > at
> >
> com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
> > at
> >
> com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:195)
> > ... 35 more
> >
> >
> > Any help would be appreciated. Thanks.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> <mailto:users-unsubscribe_at_jersey.dev.java.net>
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
> <mailto:users-help_at_jersey.dev.java.net>
>
>

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