1

I'm working on a RestWebService using Resteasy. The basic implementation works fine. Know I tried to return a Complexer- Object through rest... Actually its pretty easy..I thought. I'm getting a problem because of my nested object (Address)...

What I try is this:

@XmlRootElement(name = "person")
@XmlAccessorType(XmlAccessType.FIELD)
public class Person implements Serializable {
    private static final long serialVersionUID = 1199647317278849602L;
    private String uri;
    private String vName;
    private String nName;
    private Address address;

        .....

        @XmlElementWrapper(name="Former-User-Ids")
    @XmlElement(name="Adress")
    public Address getAddress() {
        return address;
    }
....

Address looks like this:

@XmlRootElement(name = "address")
@XmlAccessorType(XmlAccessType.FIELD)
public class Address {
    private String uri;
    private String street;
    private String city;

public String getCity() {
    return city;
}

public String getStreet() {
    return street;
}

.... The Restservice looks like this. It worked perfect without the address object..

    @Path("/getPersonXML/{personNumber}")
    @GET
    @Produces(MediaType.APPLICATION_XML)
    public Patient getPatientXML(@PathParam("personNumber") String personNumber) throws ParseException {

        Address a1 = new Address("de.person/address/" + "432432","Teststret12","TestCity", "32433", "TestCountry", "081511833");
        Patient p1 = new Person();
        p1.setAddress(a1);
        p1.setUri("de.spironto/person/"+ "432432");
        p1.setnName("Power");
        p1.setvName("Max");
        return p1;
    }

At the moment I'm always getting a

javax.xml.bind.JAXBException:

Any Ideas?

Taryn
  • 242,637
  • 56
  • 362
  • 405
Constantin Treiber
  • 420
  • 1
  • 7
  • 18

2 Answers2

1

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

PROBLEM

The @XmlElementWrapper annotation must be used with a collection property. This means you can have:

@XmlElementWrapper
public List<PhoneNumber> getPhoneNumbers() {
    return phoneNumbers;
}

But not

@XmlElementWrapper
public Address getAddress() {
    return address;
}

SOLUTION #1 - Using Any JAXB Proivder

You could use an XmlAdapter to accomplish this (see linked answer below):

SOLUTION #2 - Using EclipseLink JAXB (MOXy)

You could leverage the @XmlPath extension to map this use case:

@XmlPath("Former-User-Ids/Address")
public Address getAddress() {
    return address;
}

For More Information

Community
  • 1
  • 1
bdoughan
  • 147,609
  • 23
  • 300
  • 400
0

After building a small marshaller test. I got the failure that there are several properties with the same name. So I tried to delete all @XML_Eleemets annotations in the Address class. That worked for me...

Constantin Treiber
  • 420
  • 1
  • 7
  • 18