简体   繁体   中英

How to serialize a List with JAXB and XmlJavaTypeAdapter?

I have a List<String> that I want to serialize using JAXB . The list is also preprocessed by some XmlJavaTypeAdapter as follows:

@XmlElement(name = "category")
@XmlJavaTypeAdapter(AdapterXml.class) //for modifying some values inside the list during serialization
private List<String> categories;

Result:

Caused by: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
java.util.List is an interface, and JAXB cannot process interfaces.
    this problem is related to the following location:
        at java.util.List

Why? How can I serialize the list?

解决方案是使用AdapterXml<ArrayList<String>, ArrayList<String>>而不是仅实现List接口。

You should not use annotation @XmlJavaTypeAdapter(AdapterXml.class). Try with below

@XmlElement(name = "category")
//for modifying some values inside the no need any special type of annotation.
list during serialization
private List<String> categories;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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