2

I have a base class declared something like:

package com.mystuff.surrogates;
import java.io.Serializable;
import java.util.UUID;

public class BaseClass implements Serializable {
    private UUID id;
    private String name;

    public UUID getId() { return this.id; }
    public void setId(UUID id) { this.id = id; }
    public String getName() { return thisname; }
    public void setName(String name) { this.name = name; }
}

And a derived class which looks something like:

package com.mystuff.surrogates;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class DerivedClass extends BaseClass {
    private String email;

    public String getEmail() { return this.email; }
    public void setEmail(String email) { this.email = email; }
}

Finally I have a class which I am trying to return as an object from a RESTful webservice call which includes a collection of BaseClass derived class instances and looks rather like:

package com.mystuff.surrogates;

import java.util.ArrayList;
import java.util.List;

public class Response {
    List<BaseClass> objectList;

    public List<BaseClass> getObjectList() { return this.objectList; }
    public void setObjectList(List<BaseClass> objectList) { this.objectList = objectList; }

    public void addObject(BaseClass obj) {
        if (this.objectList == null) {
            this.objectList = new ArrayList<>();
        }

        this.objectList.add(obj);
    }
}

When marshaling this into either XML or JSON, only the members in the base class are included. How do I get Jersey / MOXy to marshal the entire class instance rather than just the base class members? While I have only shown here one derived class, I have several others I would like to potentially chose from to return in the list, so simply using List<DerivedClass> isn't an option since it would preclude returning any of those other classes in the list.

Other information:

  • Netbeans 8.0.2
  • Glassfish 4.1 (locally hosted)
  • Oracle JDK 8U31 (64 bit Windows)
  • Java EE 7
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
andand
  • 17,134
  • 11
  • 53
  • 79

1 Answers1

1

You'll need to use @XmlSeeAlso so the other classes are binded.

@XmlSeeAlso({DerivedClass.class})
public class BaseClass {

This may not get you the exact desired result, as the marshalled data will have a reference to the type. For instance with XML, you will see

<objectList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="derivedClass">

and JSON you will see

{"objectList":[{"type":"derivedClass",...
  • You can have a look at this answer for an idea of how to get rid of the type property if it's undesired.

As far as the JSON is concerned, using Jackson will not have this behavior. You can simply use jersey-media-json-jackson, which Glassfish also comes shipped with (you can add it as a dependency in a provided scope), and just register the JacksonFeature with the application.

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • I wasn't able to get the `@XmlSeeAlso` directive to cause MOXy to behave differently. But using Jackson seems to have fixed things nicely, though. – andand Feb 11 '15 at 02:53