Search code examples
javajakarta-eejaxbjava-ee-6jaxb2

JAXB equivalent of the JPA @Embdedded annotation


have a quick question:

I have the following two classes.

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {

  @XmlElement(name = "name")
  private String name;

  @XmlElement(name = "address")
  private Address address;

}

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Address {

  @XmlElement(name = "post_code")
  private String postCode;

  @XmlElement(name = "country")
  private String country;
}

This produces the following JSON:

{ 
  "name" : "john",
  "address" : {
      "post_code" : "XYZ 123",
      "country" : "US"
  }
}

But I actually want something more like this (so its similar in nature to the @Embedded in JPA):

{ 
  "name" : "john",
  "post_code" : "XYZ 123",
  "country" : "US"
}

So the embedded object fields are 'pulled up'. I noticed on stackoverflow, it was mentioned that you can use MOXy's @XmlPath(".") to do this. But is there no way from the standard java EE spec to do this and not a MOXy specific annotation?


Solution

  • There currently isn't a standard JAXB (JSR-222) annotation to accomplish what can be done with EclipseLink JAXB (MOXy)'s @XmlPath(".") annotation as of JAXB 2.2.