users@jersey.java.net

Re: [Jersey] Optional JAXB arguments to _at_POST requests

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 07 Apr 2009 10:15:42 +0200

Hi Richard,

This is a bug both in Jersey and JAX-RS. See issue 154:

   https://jersey.dev.java.net/issues/show_bug.cgi?id=154

You can use the String type and check if the String is empty or not,
if the latter then you can unmarshal yourself.

I do not currently have a satisfactory solution to solve this, the
absence of an XML-based entity body should not really have any XML-
related media type associated with it (because there is no XML
document) so strictly speaking the client should not be sending any
Content-Type header with the POST request.

One workaround is for the client to send some XML document say:

   <inputDevice/>

that signals default behavior.

Another, if you choose to send the POST request with no content-type
is to have a @Consumes("application/octet-stream") as Jersey/JAX-RS
will default to "application/octet-stream" in the absence of a media
type. Ideally what JAX-RS requires is the matching for a request that
has no request entity body.

Paul.

On Apr 7, 2009, at 3:06 AM, Richard Bailey wrote:

> With the following code:
>
> @Path("{deviceId}/")
> @POST
> @Consumes({"application/json", "application/xml"})
> public ResponseConverter delegatePost (
> @PathParam("deviceId") Integer devId,
> InputDevice inputDevice) {
> ...
> }
>
> InputDevice is an object annotated with XmlRootElement and I'm using
> JAXB to marshal it to/from json & xml.
>
> My problem is that I don't want to require that the InputDevice xml
> or json to be present and use some defaults if it's not... but if I
> don't provide one as part of the http body this exception is getting
> thrown[0]. Is there any way to do this or is it a lost cause?
>
> thanks,
> -Richard
>
> [0]: SEVERE: StandardWrapperValve[ServletAdaptor]: PWC1406:
> Servlet.service() for servlet ServletAdaptor threw exception
> com.sun.jersey.api.container.MappableContainerException:
> java.io.IOException: Error unmarshalling JAXB object of type "class
> edu.gatech.rnoc.whereami.converter.custom.consumer.InputDevice".
> at
> com
> .sun
> .jersey
> .spi.container.ContainerRequest.getEntity(ContainerRequest.java:395)
> at
> com
> .sun
> .jersey.server.impl.model.method.dispatch.EntityParamDispatchProvider
> $EntityInjectable.getValue(EntityParamDispatchProvider.java:81)
> at
> com
> .sun
> .jersey.server.impl.model.method.dispatch.EntityParamDispatchProvider
> $EntityParamInInvoker.getParams(EntityParamDispatchProvider.java:99)
> at
> com
> .sun
> .jersey.server.impl.model.method.dispatch.EntityParamDispatchProvider
> $TypeOutInvoker._dispatch(EntityParamDispatchProvider.java:136)
> at
> com
> .sun
> .jersey
> .server
> .impl
> .model
> .method
> .dispatch
> .ResourceJavaMethodDispatcher
> .dispatch(ResourceJavaMethodDispatcher.java:67)
> at
> com
> .sun
> .jersey
> .server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:154)
> at
> com
> .sun
> .jersey
> .server
> .impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:71)
> at
> com
> .sun
> .jersey
> .server
> .impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:111)
> at
> com
> .sun
> .jersey
> .server
> .impl
> .uri
> .rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:63)
> at
> com
> .sun
> .jersey
> .server
> .impl
> .application
> .WebApplicationImpl._handleRequest(WebApplicationImpl.java:543)
> at
> com
> .sun
> .jersey
> .server
> .impl
> .application
> .WebApplicationImpl.handleRequest(WebApplicationImpl.java:502)
> at
> com
> .sun
> .jersey
> .server
> .impl
> .application
> .WebApplicationImpl.handleRequest(WebApplicationImpl.java:493)
> at
> com
> .sun
> .jersey.spi.container.servlet.WebComponent.service(WebComponent.java:
> 308)
> at
> com
> .sun
> .jersey
> .spi
> .container.servlet.ServletContainer.service(ServletContainer.java:314)
> at
> com
> .sun
> .jersey
> .spi
> .container.servlet.ServletContainer.service(ServletContainer.java:239)
> at
> org
> .apache
> .catalina
> .core
> .ApplicationFilterChain.servletService(ApplicationFilterChain.java:
> 431)
> at
> org
> .apache
> .catalina
> .core.StandardWrapperValve.preInvoke(StandardWrapperValve.java:462)
> at
> org
> .apache
> .catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:
> 139)
> at
> org
> .apache
> .catalina.core.StandardContextValve.invoke(StandardContextValve.java:
> 186)
> at
> org
> .apache
> .catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:719)
> at
> org
> .apache
> .catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:657)
> at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:
> 96)
> at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke
> (PESessionLockingStandardPipeline.java:98)
> at
> org
> .apache
> .catalina.core.StandardHostValve.invoke(StandardHostValve.java:187)
> at
> org
> .apache
> .catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:719)
> at
> org
> .apache
> .catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:657)
> at
> org
> .apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:
> 651)
> at
> org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1030)
> at
> org
> .apache
> .catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:
> 142)
> at
> org
> .apache
> .catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:719)
> at
> org
> .apache
> .catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:657)
> at
> org
> .apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:
> 651)
> at
> org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1030)
> at
> org
> .apache
> .catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:325)
> at
> org
> .apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:
> 242)
> at
> com
> .sun
> .enterprise
> .v3.services.impl.ContainerMapper.service(ContainerMapper.java:180)
> at
> com
> .sun
> .grizzly
> .http.DefaultProcessorTask.invokeAdapter(DefaultProcessorTask.java:
> 633)
> at
> com
> .sun
> .grizzly
> .http.DefaultProcessorTask.doProcess(DefaultProcessorTask.java:570)
> at
> com
> .sun
> .grizzly.http.DefaultProcessorTask.process(DefaultProcessorTask.java:
> 827)
> at
> com
> .sun
> .grizzly
> .http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:152)
> at
> com
> .sun
> .enterprise
> .v3
> .services
> .impl
> .GlassfishProtocolChain
> .executeProtocolFilter(GlassfishProtocolChain.java:71)
> at
> com
> .sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:
> 103)
> at
> com
> .sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:
> 89)
> at
> com
> .sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
> at
> com
> .sun
> .grizzly
> .ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:67)
> at
> com
> .sun
> .grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:56)
> at
> com
> .sun.grizzly.util.WorkerThreadImpl.processTask(WorkerThreadImpl.java:
> 325)
> at
> com.sun.grizzly.util.WorkerThreadImpl.run(WorkerThreadImpl.java:184)
> Caused by: java.io.IOException: Error unmarshalling JAXB object of
> type "class
> edu.gatech.rnoc.whereami.converter.custom.consumer.InputDevice".
> at
> com
> .sun
> .jersey
> .impl
> .provider
> .entity
> .AbstractRootElementProvider
> .readFrom(AbstractRootElementProvider.java:91)
> at
> com
> .sun
> .jersey
> .spi.container.ContainerRequest.getEntity(ContainerRequest.java:391)
> ... 47 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
> .bind
> .v2
> .runtime
> .unmarshaller
> .UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:510)
> at
> com
> .sun
> .xml
> .bind
> .v2
> .runtime
> .unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:215)
> at
> com
> .sun
> .xml
> .bind
> .v2
> .runtime
> .unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:184)
> at
> javax
> .xml
> .bind
> .helpers
> .AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)
> at
> javax
> .xml
> .bind
> .helpers
> .AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:184)
> at
> com
> .sun
> .jersey
> .impl
> .provider
> .entity.XMLRootElementProvider.readFrom(XMLRootElementProvider.java:
> 92)
> at
> com
> .sun
> .jersey
> .impl
> .provider
> .entity
> .AbstractRootElementProvider
> .readFrom(AbstractRootElementProvider.java:89)
> ... 48 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)
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>