【发布时间】:2020-01-14 09:56:41
【问题描述】:
给定以下类:
@XmlRootElement(name = "Person")
@AutoValue
@CopyAnnotations
public abstract class Person {
@XmlElement
abstract String name();
public static Builder builder() {
return new AutoValue_Person.Builder();
}
@AutoValue.Builder
public abstract static class Builder {
public abstract Builder name(String name);
public abstract Person build();
}
}
当我跑步时:
Person person = Person.builder().name("Test").build();
StringWriter stringWriter = new StringWriter();
JAXB.marshal(person, stringWriter);
String xmlContent = stringWriter.toString();
System.out.println(xmlContent);
我总是得到:
com.example.commands.AutoValue_Person does not have a no-arg default constructor.
this problem is related to the following location:
at com.example.commands.AutoValue_Person
JAXB annotation is placed on a method that is not a JAXB property
this problem is related to the following location:
at @javax.xml.bind.annotation.XmlElement(name=##default, namespace=##default, type=class javax.xml.bind.annotation.XmlElement$DEFAULT, required=false, defaultValue=�, nillable=false)
at com.example.commands.AutoValue_Person
我想让它工作而无需按照http://blog.bdoughan.com/2010/12/jaxb-and-immutable-objects.html 中的建议创建适配器。我有太多的数据对象,我不想复制它们中的每一个。奇怪的是,在 GitHub 中似乎有大量使用 JAXB 的 AutoValue 而不使用适配器:https://github.com/search?q=XmlRootElement+autovalue&type=Code
【问题讨论】:
-
我自己现在也面临同样的问题。如果您找到答案,请告诉我们。如果我先找到它,我也会这样做。
标签: java jaxb auto-value