简体   繁体   English

XmlAdapter和JSON的MOXy异常

[英]MOXy exception with XmlAdapter and JSON

I'm running MOXy 2.5.0 and getting a ClassCastException with an XmlAdapter , but only when trying to output JSON; 我运行莫西2.5.0和得到一个ClassCastExceptionXmlAdapter ,而只是试图输出JSON时; the XML output works fine. XML输出工作正常。

I've got a wrapper class that looks like this: 我有一个包装器类,如下所示:

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

There are descendants of this class that look like this: 该类的后代如下所示:

@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();
    }
}

There is then an XmlAdapter for RecordList that looks like this: 然后有一个RecordList的XmlAdapter看起来像这样:

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

Finally there is a class that contains members like RecordList and declares them as such: 最后是一个包含RecordList类的成员的类,并按如下方式声明它们:

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

    // ... other stuff ...
}

With the reference JAXB implementation this works fine in both XML and JSON (we were using Jackson for the JSON logic). 使用参考JAXB实现,在XML和JSON中都可以正常工作(我们将Jackson用作JSON逻辑)。 With a change to MOXy (and removal of Jackson) I'll get an exception trace similar to the following when asking for JSON output. 更改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)

I'm not sure why MOXy is trying to tread a Record as a RecordList , but it appears as though that's what is happening. 我不知道为什么MOXy试图将Record用作RecordList ,但是似乎就在发生这种情况。 Like I said, this does not happen with the reference (ie Sun) implementation of JAXB; 就像我说的那样,JAXB的引用(即Sun)实现不会发生这种情况。 it only happens when I'm trying to use MOXy. 它仅在我尝试使用MOXy时发生。

Any ideas here? 这里有什么想法吗? Thanks. 谢谢。

After you removed Jackson you no longer have a write adapter for JSON. 删除Jackson后,您将不再具有JSON的写适配器。 You need to tell the server how it should produce JSON. 您需要告诉服务器它应该如何产生JSON。

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group. 注意:我是EclipseLink JAXB(MOXy)的负责人,并且是JAXB(JSR-222)专家组的成员。

One difference between MOXy and the other technologies that you are using is that MOXy will treat RecordList and ListWrapper as a java.util.List as it implements java.util.List . 莫西和您正在使用其他技术之间的一个区别是,莫西将把RecordListListWrapper作为java.util.List ,因为它实现了java.util.List As such a XmlAdapter can not be applied to it, as it can only apply to items in the collection. 因此,不能将XmlAdapter应用于它,因为它只能应用于集合中的项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM