Question

I am using atmosphere jersey configured using servlet, on the post of my atmosphere Handler I am receiving an abstract object of different types of commands, base on class type I am redirecting to a worker.

I have an abstract class Command and My different commands extends from it

  • Start
  • Continue
  • End

in the post I wanted to route to a different worker

@POST
def incoming(@PathParam("topic") topic: String, cmd: Command) {
  cmd.getCmdType match {
  case "Start" =>
    cmd.asInstanceOf[Start]
    //
  case "Continue" =>
    cmd.asInstanceOf[Continue]
  case "End" =>
    cmd.asInstanceOf[End]
}

Command.java

@XmlRoot
@JsonTypeInfo(
    use = JsonTypeInfo.Id.NAME,
    include = JsonTypeInfo.As.WRAPPER_OBJECT,
    property = "type")
@JsonSubTypes({
    @Type(value = Start.class, name = "Start"),
    @Type(value = Continue.class, name = "Continue"),
    @Type(value = End.class, name = "End")})
public abstract class Command

I configured jersey

  <init-param>
     <param-name>com.sun.jersey.config.property.packages</param-name>
     <param-value>com.mypackage</param-value>
  </init-param>
  <init-param>
     <param-name>org.atmosphere.websocket.messageContentType</param-name>
     <param-value>application/json</param-value>
  </init-param>

But when I try to push to the socket

    var msgJson = {user: userId, eventCode: event, type: "Start"};
    var msg = JSON.stringify(msgJson);
    subSocket.push(msg);

Complains saying doesn't know how to instantiate the abstract class. So it's ignoring my subtypes annotation

Caused by: javax.xml.bind.UnmarshalException: null
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStreamException(UnmarshallerImpl.java:431) ~[jaxb-impl-2.2.3-1.jar:2.2.3]
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:368) ~[jaxb-impl-2.2.3-1.jar:2.2.3]
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:345) ~[jaxb-impl-2.2.3-1.jar:2.2.3]
    at com.sun.jersey.json.impl.BaseJSONUnmarshaller.unmarshalJAXBElementFromJSON(BaseJSONUnmarshaller.java:111) ~[jersey-json-1.14.jar:1.14]
    at com.sun.jersey.json.impl.BaseJSONUnmarshaller.unmarshalFromJSON(BaseJSONUnmarshaller.java:100) ~[jersey-json-1.14.jar:1.14]
    at com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider.readFrom(JSONRootElementProvider.java:129) ~[jersey-json-1.14.jar:1.14]
    at com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.readFrom(AbstractRootElementProvider.java:111) ~[jersey-core-1.14.jar:1.14]
    ... 41 common frames omitted
Caused by: com.sun.istack.SAXParseException2: Unable to create an instance of mypackage.Command
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:647) ~[jaxb-impl-2.2.3-1.jar:2.2.3]
    at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:258) ~[jaxb-impl-2.2.3-1.jar:2.2.3]
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.createInstance(UnmarshallingContext.java:614) ~[jaxb-impl-2.2.3-1.jar:2.2.3]
    at com.sun.xml.bind.v2.runtime.unmarshaller.StructureLoader.startElement(StructureLoader.java:185) ~[jaxb-impl-2.2.3-1.jar:2.2.3]
    at com.sun.xml.bind.v2.runtime.unmarshaller.XsiTypeLoader.startElement(XsiTypeLoader.java:80) ~[jaxb-impl-2.2.3-1.jar:2.2.3]
    at com.sun.xml.bind.v2.runtime.unmarshaller.ProxyLoader.startElement(ProxyLoader.java:60) ~[jaxb-impl-2.2.3-1.jar:2.2.3]
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:486) ~[jaxb-impl-2.2.3-1.jar:2.2.3]
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:464) ~[jaxb-impl-2.2.3-1.jar:2.2.3]
    at com.sun.xml.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(InterningXmlVisitor.java:75) ~[jaxb-impl-2.2.3-1.jar:2.2.3]
    at com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.handleStartElement(StAXStreamConnector.java:247) ~[jaxb-impl-2.2.3-1.jar:2.2.3]
    at com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(StAXStreamConnector.java:181) ~[jaxb-impl-2.2.3-1.jar:2.2.3]
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:366) ~[jaxb-impl-2.2.3-1.jar:2.2.3]
    ... 46 common frames omitted
Caused by: javax.xml.bind.UnmarshalException: Unable to create an instance of mypackage.AbstractCommand
    ... 58 common frames omitted
Caused by: java.lang.InstantiationException: null
    at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:48) ~[na:1.7.0_15]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525) ~[na:1.7.0_15]
    at com.sun.xml.bind.v2.ClassFactory.create0(ClassFactory.java:122) ~[jaxb-impl-2.2.3-1.jar:2.2.3]
    at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.createInstance(ClassBeanInfoImpl.java:269) ~[jaxb-impl-2.2.3-1.jar:2.2.3]
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.createInstance(UnmarshallingContext.java:608) ~[jaxb-impl-2.2.3-1.jar:2.2.3]
    ... 55 common frames omitted
Was it helpful?

Solution

Turned out that using JAXB and Jackson was confusing jersey. Shuffled things around and couldn't make it work using @XmlAlsoSee and so on. I found a way to using only Jackson annotations and it works

removed JAXB annotation

@JsonTypeInfo(
    use = JsonTypeInfo.Id.NAME,
    include = JsonTypeInfo.As.WRAPPER_OBJECT,
    property = "type")
@JsonSubTypes({
    @Type(value = Start.class, name = "Start"),
    @Type(value = Continue.class, name = "Continue"),
    @Type(value = End.class, name = "End")})
public abstract class Command

added extra parameter to servlet in web.xml this tells to use Jackson only and works like a charm

  <init-param>
     <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
     <param-value>true</param-value>
  </init-param>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top