1

我正在运行 MOXy 2.5.0 并获得 a ClassCastExceptionXmlAdapter但仅在尝试输出 JSON 时;XML 输出工作正常。

我有一个看起来像这样的包装类:

@XmlAccessorType(XmlAccessType.NONE)
public abstract class ListWrapper<T> implements List<T>
{
    private List<T> list = new ArrayList<T>();

    @XmlElement
    private Foo foo;  // stuff specific to my problem domain

    public ListWrapper()
    {
    }

    public ListWrapper(Foo foo, List list)
    {
        this.foo = foo;
        this.list = list;
    }

    // ... implementation of List<T> using the list member ...
}

这个类的后代看起来像这样:

@XmlRootElement(name = "recordList")
@XmlAccessorType(XmlAccessType.NONE)
public class RecordList extends ListWrapper<Record>
{
    public RecordList()
    {
        super();
    }

    public RecordList(Foo foo, List<Record> list)
    {
        super(foo, list);
    }

    @Override
    @XmlElementWrapper(name = "records")
    @XmlElement(name = "record", type = Record.class)
    public List<Record> getList()
    {
        return super.getList();
    }
}

然后有一个 XmlAdapterRecordList看起来像这样:

public class RecordListAdapter extends XmlAdapter<RecordList, RecordList>
{
    @Override
    public RecordList unmarshal(RecordList v) throws Exception
    {
        return v;
    }

    @Override
    public RecordList marshal(RecordList v) throws Exception
    {
        return v;
    }
}

最后有一个类包含成员 likeRecordList并声明它们:

@XmlRootElement(name = "container")
@XmlAccessorType(XmlAccessType.NONE)
public class Container
{
    @XmlElement
    @XmlJavaTypeAdapter(RecordListAdapter.class)
    protected RecordList recordList;

    // ... other stuff ...
}

使用参考 JAXB 实现,这在 XML 和 JSON 中都可以正常工作(我们将 Jackson 用于 JSON 逻辑)。更改 MOXy(并删除 Jackson)后,在请求 JSON 输出时,我将获得类似于以下内容的异常跟踪。

javax.xml.bind.MarshalException - with linked exception:[Exception [EclipseLink-25003] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.XMLMarshalException
Exception Description: An error occurred marshalling the object
Internal Exception: Exception [EclipseLink-3001] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.ConversionException
Exception Description: The object [1], of class [class com.example.Record], could not be converted to [class com.example.RecordList].
Internal Exception: java.lang.ClassCastException: com.example.Record cannot be cast to com.example.RecordList]
    at org.eclipse.persistence.jaxb.JAXBMarshaller.marshal(JAXBMarshaller.java:403)
...
Caused by: Exception [EclipseLink-25003] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.XMLMarshalException
Exception Description: An error occurred marshalling the object
Internal Exception: Exception [EclipseLink-3001] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.ConversionException
Exception Description: The object [1], of class [class com.example.Record], could not be converted to [class com.example.RecordList].
Internal Exception: java.lang.ClassCastException: com.example.Record cannot be cast to com.example.RecordList
    at org.eclipse.persistence.exceptions.XMLMarshalException.marshalException(XMLMarshalException.java:97)
...
Caused by: Exception [EclipseLink-3001] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.ConversionException
Exception Description: The object [1], of class [class com.example.Record], could not be converted to [class com.example.RecordList].
Internal Exception: java.lang.ClassCastException: com.example.Record cannot be cast to com.example.RecordList
    at org.eclipse.persistence.exceptions.ConversionException.couldNotBeConverted(ConversionException.java:87)
...
Caused by: java.lang.ClassCastException: com.example.Record cannot be cast to com.example.RecordList
    at com.example.RecordListAdapter.marshal(RecordListAdapter.java:5)
    at org.eclipse.persistence.internal.jaxb.XMLJavaTypeConverter.convertObjectValueToDataValue(XMLJavaTypeConverter.java:172)

我不确定 MOXy 为何试图将 aRecord视为 a RecordList,但似乎正在发生这种情况。就像我说的,JAXB 的参考(即 Sun)实现不会发生这种情况。它仅在我尝试使用 MOXy 时发生。

这里有什么想法吗?谢谢。

4

2 回答 2

0

注意: 我是EclipseLink JAXB (MOXy)负责人,也是JAXB (JSR-222)专家组的成员。

MOXy 与您正在使用的其他技术之间的一个区别是 MOXy 将RecordListListWrapper视为java.util.List它实现的java.util.List。像这样的 aXmlAdapter不能应用于它,因为它只能应用于集合中的项目。

于 2013-11-11T17:10:24.180 回答
0

删除 Jackson 后,您不再拥有 JSON 的写入适配器。您需要告诉服务器它应该如何生成 JSON。

于 2013-11-08T16:21:45.063 回答