membersound
membersound

Reputation: 86925

How to name JAXB classes?

@XmlRootElement
public class MyRoot {
    private List<SubRootDTO> subs;
}

public class SubRootDTO {

}

How can I give an explicit JAXB name to the SubRootDTO class?

Upvotes: 1

Views: 70

Answers (3)

My-Name-Is
My-Name-Is

Reputation: 4940

@XmlRootElement
class MyRoot {
    private List<SubRootDTO> subs;

    ...

    @XmlElementRef(name = "CustomName")
    public List<SubRootDTO> getSubs(){
        return subs;
    }
}

class SubRootDTO {

}

In contrast to annotate SubRootDTO with @XmlRootElement(name = "XYZ"), @XmlElementRef(name = "ABC") allows you to name the element differently for each reference.

Upvotes: 0

bdoughan
bdoughan

Reputation: 149047

There are a couple of different options:

  1. Annotate SubRootDto with @XmlRootElement and then use @XmlElementRef on all mapped fields/properties that reference it.
  2. Annotate the SubRootDto properties with @XmlElement.

Upvotes: 1

Smutje
Smutje

Reputation: 18173

@XmlRootElement(name = "subRoot")
public class SubRootDTO {
}

Upvotes: 0

Related Questions