3

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 . 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

Humble Student
  • 3,755
  • 4
  • 20
  • 35
  • 1
    I'm facing this same problem myself right now. If you find an answer, let us know. I will do the same if I find it first. – E.S. Jan 14 '20 at 18:39

1 Answers1

1

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

Notice the naming structure used in the sample AutoValue Java class, it still uses the getVal and setVal

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. Try to refactor your code to those conventions.

E.S.
  • 2,733
  • 6
  • 36
  • 71
  • 1
    Yes, I also figured this out this yesterday but forgot to update the question. Having the `get` prefix in the your AutoValue class is enough (no need for `set` in the Builder). Looks like JAXB recognises a property based on the accessor name. – Humble Student Jan 15 '20 at 07:48