我正在运行MOXy 2.5.0并获得ClassCastException
XmlAdapter
,但仅在尝试输出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();
}
}
然后有一个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;
}
}
最后,有一个类包含RecordList
之类的成员,并将其声明为:
@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试图将Record
作为RecordList
,但看起来好像正在发生的事情。就像我说的那样,这不会发生在JAXB的引用(即Sun)实现上;它只发生在我尝试使用MOXy时。
这里有什么想法吗?感谢。
答案 0 :(得分:0)
删除Jackson后,您不再拥有JSON的写入适配器。您需要告诉服务器它应该如何生成JSON。
答案 1 :(得分:0)
注意:我是EclipseLink JAXB (MOXy)主管,是JAXB (JSR-222)专家组的成员。
MOXy与您正在使用的其他技术之间的一个区别是,MOXy会将RecordList
和ListWrapper
视为java.util.List
,因为它会实现java.util.List
。因此,XmlAdapter
无法应用于它,因为它只能应用于集合中的项目。