【问题标题】:Marsheling error "does not have a no-arg default constructor" when using JAXB with AutoValue将 JAXB 与 AutoValue 结合使用时,编组错误“没有无参数默认构造函数”
【发布时间】: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


【解决方案1】:

在研究了你的 github 链接后,我实际上意识到了这个问题,特别是这个:https://github.com/google/nomulus/blob/8f2a8835d7f09ad28806b2345de8d42ebe781fe6/core/src/main/java/google/registry/model/contact/ContactInfoData.java

注意示例 AutoValue Java 类中使用的命名结构,它仍然使用 getValsetVal

这是一个基于我现在可以工作的代码的简单示例:

import com.google.auto.value.AutoValue;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement(name = "Customer")
@XmlType(propOrder = {"name", "age", "id"})
@AutoValue.CopyAnnotations
@AutoValue
public abstract class Customer {

    public static Builder builder() {
        return new AutoValue_Customer.Builder();
    }

    @XmlElement(name = "name")
    abstract String getName();

    @XmlElement(name = "age")
    abstract int getAge();

    @XmlAttribute(name = "id")
    abstract int getId();

    @AutoValue.Builder
    public abstract static class Builder {
        public abstract Builder setName(String name);

        public abstract Builder setAge(int age);

        public abstract Builder setId(int id);

        public abstract Customer build();
    }
}

请注意,我在构建器中使用setName(String name),但在类本身中使用String getName()。尝试将您的代码重构为这些约定。

【讨论】:

  • 是的,我昨天也想到了这个,但忘记更新问题了。在 AutoValue 类中有 get 前缀就足够了(在 Builder 中不需要 set)。看起来 JAXB 根​​据访问器名称识别属性。
猜你喜欢
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-22
  • 1970-01-01
  • 2012-07-14
  • 2016-03-28
相关资源
最近更新 更多