views:

268

answers:

1

I am writing a Resteasy server application and am having trouble getting my superclasses to marshal. I have code something like this:

@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name = "person")
class Person {
  protected String name;

  @XmlElement(name = "name")
  public String getName() { return name; }

  public void setName(String name) { this.name = name; }
}

@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name = "employee")
class Employee extends Person {
  protected Integer id;

  @XmlElement(name = "id")
  public Integer getId() { return id; }

  public void setId(Integer id) { this.id = id; }
}

When I marshal the Employee class to XML, I get something like this:

<employee>
  <id>12345</id>
</employee>

with no output of the name field inherited from the Person class.

What am I doing wrong?

Thanks, Ralph

A: 

I'm not sure how you're configuring the JAXB context or marshaller but the following:-

public static void main(String[] args) throws Exception
{

        Employee employee = new Employee();
        employee.setId(1);
        employee.setName("Ralph");

        JAXBContext context = JAXBContext.newInstance(Employee.class);
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(employee, System.out);

}

gives:-

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee>
    <name>Ralph</name>
    <id>1</id>
</employee>
Strawberry
I'm not, actually. I am running code similar to:return Response.ok(new GenericEntity<List<Employee>>(listOfEmployees).build();from my JAX-RS URL handler, because I need to be able to return other content headers in the REST response, and that was the only way I could find to return an ArrayList of Employees. I never explicitly created a JAXBContext or a Marshaller. Is there a better way? Maybe I'll try feeding the output of the marshaller to the Response.ok() method.
Ralph
Sorry I'm not familiar with JAX-RS.
Strawberry
Boy, that was a DUE (dumb user error). I created an annotation to fill fields from database columns (private implementation of "ORM"), but was not traversing the superclass fields, so they were all null. It works now.
Ralph