users@jersey.java.net

RE: [Jersey] Error when unmarshalling abstract property via JSON

From: Peter Hill <Peter.Hill_at_tudor.com>
Date: Thu, 9 Sep 2010 15:37:21 +0000

Hi Paul,

Thank you very much for your response. I am new to Jersey and JSON, so I apologise if I have misunderstood but by creating a custom ContextResolver (as you suggested), I have been able to un-marshal the diagram objects correctly, but I have had to use the badgerfish notation (example below). I would prefer not to use badgerfish if possible, should it be possible to marshal/un-marshal this object with the default notation?

@Provider
public class DiagramJAXBContextProvider implements ContextResolver<JAXBContext> {

        private JAXBContext context;
        private Class[] types = { Diagram.class };

        public DiagramJAXBContextProvider() throws Exception {
                this.context = new JSONJAXBContext(JSONConfiguration.badgerFish().build(),
                                types);
        }

        public JAXBContext getContext(Class<?> objectType) {
                for (Class type : types) {
                        if (type.equals(objectType))
                                return context;
                }
                return null;
        }
}

Thanks again,
Peter

-----Original Message-----
From: Paul Sandoz [mailto:Paul.Sandoz_at_oracle.com]
Sent: 09 September 2010 10:55
To: users_at_jersey.dev.java.net
Cc: Peter Hill
Subject: Re: [Jersey] Error when unmarshalling abstract property via JSON

Hi Peter,

I think this is because in the absence of you providing a JAXB context Jersey creates a JAXB context from the Diagram class, which may not have all the necessary information about the concrete types.

So you may need to implement a ContextResolver<JAXBContext>. See here:

   https://jersey.dev.java.net/nonav/documentation/1.4-SNAPSHOT/user-guide.html
#d4e848


Pavel, i think we need to explain in the documentation what the
default behavior is for Jersey, and also explain why one might need to
use ContextResolver<JAXBCOntext>. Also JAXBContext<Marshaller>
JAXBContext<Unmarshaller> are also supported, and these are
advantageous when properties need to be set when un/marshaling. (As
you know we also need to document support for List<T>).

Paul.

On Sep 7, 2010, at 5:12 PM, Peter Hill wrote:

> Hi,
>
> I am having trouble un-marshalling a list of objects when the object
> type in the list has a property which is defined as an abstract class.
>
> Below is how the classes are defined. Have I done this correctly?
> The server appears to create valid syntax. I have searched the
> internet for the last couple of days but cannot seem to find a
> solution. If I switch the transport protocol to XML I do not have
> this problem, but unfortunately I cannot use this in production.
>
> I have also included a test case which should show the error.
>
> @XmlRootElement(name="diagram")
> public class Diagram {
>
> private Shape shape;
>
> public Diagram() {
> super();
> }
>
> public Diagram(Shape shape) {
> super();
> this.shape = shape;
> }
>
> public Shape getShape() {
> return shape;
> }
>
> public void setShape(Shape shape) {
> this.shape = shape;
> }
>
> }
>
> @XmlRootElement
> @XmlSeeAlso({Square.class, Circle.class})
> public abstract class Shape {
>
> ..
>
> }
>
> @XmlRootElement
> public class Circle extends Shape {
>
> ..
>
> }
>
> Error:
> [javax.xml.bind.UnmarshalException: Unable to create an instance of
> com.diagrams.Shape
>
> I am using the latest jersey server and client jars to do this,
> running on Jboss 5.1. All other classes are communicated between the
> client and server seamlessly.
>
> Any help or guidance would be greatly appreciated.
>
> Many thanks,
> Peter
> --
>
> _________________________________________________________
>
> This email, its contents, and any attachments transmitted with it
> are intended only for the addressee(s) and may be confidential and
> legally privileged. We do not waive any confidentiality by
> misdelivery. If you have received this email in error, please notify
> the sender immediately and delete it. You should not copy it,
> forward it or otherwise use the contents, attachments or information
> in any way. Any liability for viruses is excluded to the fullest
> extent permitted by law.
>
> Tudor Capital Europe LLP (TCE) is authorised and regulated by The
> Financial Services Authority (the FSA). TCE is registered as a
> limited liability partnership in England and Wales No: OC340673 with
> its registered office at Great Burgh, Epsom, Surrey, KT18
> 5XT.<abstractpropertyexample.zip>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net

--
_________________________________________________________
This email, its contents, and any attachments transmitted with it are intended only for the addressee(s) and may be confidential and legally privileged. We do not waive any confidentiality by misdelivery. If you have received this email in error, please notify the sender immediately and delete it. You should not copy it, forward it or otherwise use the contents, attachments or information in any way. Any liability for viruses is excluded to the fullest extent permitted by law.
Tudor Capital Europe LLP (TCE) is authorised and regulated by The Financial Services Authority (the FSA). TCE is registered as a limited liability partnership in England and Wales No: OC340673 with its registered office at Great Burgh, Epsom, Surrey, KT18 5XT.