users@jersey.java.net

Re: [Jersey] JSON and abstract class

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Thu, 18 Jun 2009 13:55:36 +0200

Hi,

Refer to the concrete type, MyCustomer, in the resource method, or
make Customer non-abstract.

Otherwise...

By default, without any configuration, a JAXBContext will be derived
from the type, Customer, and it will not know anything about
MyCustomer, which is what you want instantiated.

To supply a JAXBContext you need to provide an implementation of
ContextResolver<JAXBContext>, for example, i think the following
should work for you:

   @Provider
   public final class JAXBContextResolver implements
ContextResolver<JAXBContext> {

       private final JAXBContext context;

       private final Set<Class> types;

       private final Class[] cTypes = {MyCustomer.class};

       public JAXBContextResolver() throws Exception {
           this.types = new HashSet(Arrays.asList(cTypes));
           this.context = JAXBContext.newInstance(cTypes);
       }

       public JAXBContext getContext(Class<?> objectType) {
           return (types.contains(objectType)) ? context : null;
       }
   }

Paul.

On Jun 18, 2009, at 10:48 AM, oliv wrote:

>
> Hi,
>
> I try to post an object MyCustomer extended from Customer which is an
> abstract class, and i get an error 500
>
> Here is the stack trace :
>
> GRAVE: "Servlet.service()" pour la servlet Jersey Web Application a
> g�n�r�
> une exception
> java.lang.InstantiationException
> at
> sun
> .reflect
> .InstantiationExceptionConstructorAccessorImpl.newInstance(Unknown
> Source)
> at java.lang.reflect.Constructor.newInstance(Unknown Source)
> at com.sun.xml.bind.v2.ClassFactory.create0(ClassFactory.java:123)
> at
> com
> .sun
> .xml
> .bind
> .v2.runtime.ClassBeanInfoImpl.createInstance(ClassBeanInfoImpl.java:
> 257)
> at
> com
> .sun
> .xml
> .bind
> .v2
> .runtime
> .unmarshaller
> .UnmarshallingContext.createInstance(UnmarshallingContext.java:570)
> at
> com
> .sun
> .xml
> .bind
> .v2
> .runtime
> .unmarshaller.StructureLoader.startElement(StructureLoader.java:181)
> at
> com
> .sun
> .xml
> .bind
> .v2
> .runtime.unmarshaller.XsiTypeLoader.startElement(XsiTypeLoader.java:
> 73)
> at
> com
> .sun
> .xml
> .bind
> .v2
> .runtime
> .unmarshaller
> .UnmarshallingContext._startElement(UnmarshallingContext.java:455)
> at
> com
> .sun
> .xml
> .bind
> .v2
> .runtime
> .unmarshaller
> .UnmarshallingContext.startElement(UnmarshallingContext.java:433)
> at
> com
> .sun
> .xml
> .bind
> .v2
> .runtime
> .unmarshaller
> .InterningXmlVisitor.startElement(InterningXmlVisitor.java:71)
> at
> com
> .sun
> .xml
> .bind
> .v2
> .runtime
> .unmarshaller
> .StAXStreamConnector.handleStartElement(StAXStreamConnector.java:275)
> at
> com
> .sun
> .xml
> .bind
> .v2
> .runtime
> .unmarshaller.StAXStreamConnector.bridge(StAXStreamConnector.java:209)
> at
> com
> .sun
> .xml
> .bind
> .v2
> .runtime
> .unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:358)
> at
> com
> .sun
> .xml
> .bind
> .v2
> .runtime
> .unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:330)
> at
> com
> .sun
> .jersey
> .impl
> .provider
> .entity
> .JSONRootElementProvider.readFrom(JSONRootElementProvider.java:89)
> at
> com
> .sun
> .jersey
> .impl
> .provider
> .entity
> .AbstractRootElementProvider
> .readFrom(AbstractRootElementProvider.java:89)
> at
> com
> .sun
> .jersey
> .spi.container.ContainerRequest.getEntity(ContainerRequest.java:348)
> at
> com.sun.jersey.impl.model.method.dispatch.EntityParamDispatchProvider
> $EntityInjectable.getValue(EntityParamDispatchProvider.java:81)
> at
> com.sun.jersey.impl.model.method.dispatch.EntityParamDispatchProvider
> $EntityParamInInvoker.getParams(EntityParamDispatchProvider.java:99)
> at
> com.sun.jersey.impl.model.method.dispatch.EntityParamDispatchProvider
> $TypeOutInvoker._dispatch(EntityParamDispatchProvider.java:134)
> at
> com
> .sun
> .jersey
> .impl
> .model
> .method
> .dispatch
> .ResourceJavaMethodDispatcher
> .dispatch(ResourceJavaMethodDispatcher.java:85)
> at
> com
> .sun.jersey.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:
> 123)
> at
> com
> .sun
> .jersey
> .impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:111)
> at
> com
> .sun
> .jersey
> .impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:71)
> at
> com
> .sun
> .jersey
> .impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:111)
> at
> com
> .sun
> .jersey
> .impl
> .uri
> .rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:63)
> at
> com
> .sun
> .jersey
> .impl
> .application
> .WebApplicationImpl.handleRequest(WebApplicationImpl.java:722)
> at
> com
> .sun
> .jersey
> .impl
> .application
> .WebApplicationImpl.handleRequest(WebApplicationImpl.java:692)
> at
> com
> .sun
> .jersey
> .spi
> .container.servlet.ServletContainer.service(ServletContainer.java:344)
> at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
> at
> org
> .apache
> .catalina
> .core
> .ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:
> 290)
> at
> org
> .apache
> .catalina
> .core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
> at
> org
> .apache
> .catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:
> 233)
> at
> org
> .apache
> .catalina.core.StandardContextValve.invoke(StandardContextValve.java:
> 191)
> at
> org
> .apache
> .catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
> at
> org
> .apache
> .catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
> at
> org
> .apache
> .catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:
> 109)
> at
> org
> .apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:
> 286)
> at
> org
> .apache
> .coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:857)
> at
> org.apache.coyote.http11.Http11AprProtocol
> $Http11ConnectionHandler.process(Http11AprProtocol.java:565)
> at org.apache.tomcat.util.net.AprEndpoint
> $Worker.run(AprEndpoint.java:1509)
> at java.lang.Thread.run(Unknown Source)
>
>
> My resource method is :
>
> @POST
> @Path("/custo")
> @Consumes({"application/json", "application/xml"})
> @Produces({"application/json", "application/xml"})
> public Customer updCustomer(Customer custo) {
> System.out.println("json resource POST method contacted");
> Context.getInstance().setCustomer(custo);
> return Context.getInstance().getCustomer();
> }
>
>
> Does anybody know how to solve this issue?
>
> Thanks in advance.
>
> Regards.
> Olivier.
> --
> View this message in context: http://n2.nabble.com/JSON-and-abstract-class-tp3098249p3098249.html
> Sent from the Jersey mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>