简体   繁体   English

将 JAXB 与 AutoValue 一起使用时,Marsheling 错误“没有无参数的默认构造函数”

[英]Marsheling error "does not have a no-arg default constructor" when using JAXB with AutoValue

Given the following class:鉴于以下类:

@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();
  }
}

When I run:当我运行时:

Person person = Person.builder().name("Test").build();
StringWriter stringWriter = new StringWriter();
JAXB.marshal(person, stringWriter);
String xmlContent = stringWriter.toString();
System.out.println(xmlContent);

I always get:我总是得到:

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

I would like to make it work without the need of creating an adapter as suggested in http://blog.bdoughan.com/2010/12/jaxb-and-immutable-objects.html .我想让它工作而无需按照http://blog.bdoughan.com/2010/12/jaxb-and-immutable-objects.html 中的建议创建适配器。 I have too many data objects and I don't want to duplicate each one of them.我有太多的数据对象,我不想复制它们中的每一个。 Curious enough, there seems to be tons of utilisations for AutoValue with JAXB in GitHub without the use of adapters: https://github.com/search?q=XmlRootElement+autovalue&type=Code奇怪的是,在 GitHub 中似乎有大量使用 JAXB 的 AutoValue 使用而不使用适配器: https : //github.com/search? q=XmlRootElement+autovalue&type=Code

I actually realized the problem after studying your github link, specifically this one: https://github.com/google/nomulus/blob/8f2a8835d7f09ad28806b2345de8d42ebe781fe6/core/src/main/java/google/registry/model/contact/ContactInfoData.java在研究了你的 github 链接后,我实际上意识到了这个问题,特别是这个: https : //github.com/google/nomulus/blob/8f2a8835d7f09ad28806b2345de8d42ebe781fe6/core/src/main/java/google/registry/model/contact/ContactInfoData.java

Notice the naming structure used in the sample AutoValue Java class, it still uses the getVal and setVal注意示例 AutoValue Java 类中使用的命名结构,它仍然使用getValsetVal

Here is a simple example based on my code that now works:这是一个基于我现在可以工作的代码的简单示例:

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();
    }
}

Notice I use setName(String name) in the builder but String getName() in the class itself.请注意,我在构建器中使用setName(String name)而在类本身中使用String getName() Try to refactor your code to those conventions.尝试将您的代码重构为这些约定。

暂无
暂无

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

相关问题 使用Jaxb和cxf,枚举“没有no-arg默认构造函数” - Enum “does not have a no-arg default constructor” with Jaxb and cxf JAXB 编组器无参数默认构造函数 - JAXB marshaller no-arg default constructor 什么可能导致“JAXBElement没有no-arg默认构造函数”? - What might cause “JAXBElement Does not have a no-arg default constructor”? 解组错误Jaxb-“类没有默认的无参数构造函数”,而有默认的无参数构造函数 - Unmarshalling Error Jaxb - “Class does not have a default no arg constructor” while having a default no args constructor 无法绑定到Web服务:java.lang.StackTraceElement没有无参数的默认构造函数 - Cannot bind to webservice: java.lang.StackTraceElement does not have a no-arg default constructor JAXB需要一个公共的无参数构造函数? - What JAXB needs a public no-arg constructor for? 如何修复此错误“XYZ 没有无参数构造函数” - How do I fix this error "XYZ does not have a no-arg constructor" Jackson 何时需要无参数构造函数进行反序列化? - When does Jackson require no-arg constructor for deserialization? 如果父级没有no-arg构造函数,则出现序列化问题 - Serialization issue if the parent does not have a no-arg constructor init 方法在 Spring 中工作是否需要无参数构造函数(忽略默认构造函数,因为我有一些参数构造函数)? - Is no-arg constructor(ignore default constructor as I have some arguments constructor) necessary for init method to work in Spring?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM