简体   繁体   English

如何使用JAXB和XmlJavaTypeAdapter序列化列表?

[英]How to serialize a List with JAXB and XmlJavaTypeAdapter?

I have a List<String> that I want to serialize using JAXB . 我有一个List<String>我想使用JAXB进行序列化。 The list is also preprocessed by some XmlJavaTypeAdapter as follows: 某些XmlJavaTypeAdapter也对该列表进行了预处理,如下所示:

@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). 您不应使用注释@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;

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

相关问题 列表中的JAXB XmlJavaTypeAdapter映射:此设计模式的优点/缺点 - JAXB XmlJavaTypeAdapter Map from List : pros/cons of this design pattern JAXB @XmlJavaTypeAdapter 用法 - JAXB @XmlJavaTypeAdapter usage Jaxb:将参数传递给@XmlJavaTypeAdapter - Jaxb: Passing parameter to @XmlJavaTypeAdapter 如何在JAXB中一次将@XmlJavaTypeAdapter规则应用于特定类型的所有字段? - How to apply a @XmlJavaTypeAdapter rule to all fields of a specific type at once in JAXB? 如何使用JAXB将具有不同元素名称的列表序列化为一个列表? - How to serialize a list with different element names into one list with JAXB? Glassfish 5会忽略JAXB @XmlJavaTypeAdapter注释吗? - Glassfish 5 ignores JAXB @XmlJavaTypeAdapter annotation? JAXB 未向 @XMLJavaTypeAdapter 注释添加类型 - JAXB not adding type to @XMLJavaTypeAdapter annotation 如何在某些情况下忽略/禁用/恢复/覆盖JAXB类级别的@XmlJavaTypeAdapter? - How to ignore/disable/revert/override JAXB class-level @XmlJavaTypeAdapter in certain situations? 在JAXB中为java.util.Map使用XmlJavaTypeAdapter时如何删除item元素名称 - How to get rid of item element name when using XmlJavaTypeAdapter for java.util.Map in JAXB JAXB使用@XmlJavaTypeAdapter封送Collection的子集 - JAXB using @XmlJavaTypeAdapter to marshal a subset of a Collection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM