1

我正在使用使用 servlet 配置的大气球衣,在我的大气处理程序的帖子上,我收到了不同类型命令的抽象对象,基于我重定向到工作人员的类类型。

我有一个抽象类命令,我的不同命令从它扩展

  • 开始
  • 继续
  • 结尾

在帖子中我想路由到另一个工人

@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]
}

命令.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

我配置了球衣

  <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>

但是当我尝试推送到套接字时

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

抱怨说不知道如何实例化抽象类。所以它忽略了我的子类型注释

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
4

1 回答 1

1

原来,使用 JAXB 和杰克逊是混淆球衣。乱七八糟的东西,无法使用@XmlAlsoSee 等等。我找到了一种只使用 Jackson 注释的方法,它可以工作

删除了 JAXB 注释

@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

在 web.xml 中向 servlet 添加了额外的参数,这告诉您只使用 Jackson 并且像魅力一样工作

  <init-param>
     <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
     <param-value>true</param-value>
  </init-param>
于 2013-07-17T13:32:20.157 回答