Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » GebericEntity<T> json when bootstrapping JAXBXContext
GebericEntity<T> json when bootstrapping JAXBXContext [message #1709319] Sat, 26 September 2015 04:36
David Smalley is currently offline David SmalleyFriend
Messages: 1
Registered: September 2015
Junior Member
Hello,

I am having quite a time trying to use an oxm file for jaxb serialization with collections.

I have the following trivial class:

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

    public Customer(String name) {

        id = UUID.randomUUID().toString().toUpperCase();
        this.name = name;
    }

    protected Customer() {
    }

    public String getId() {

        return id;
    }

    public String getName() {

        return name;
    }

    private static final long serialVersionUID = 300026997766496047L;

    private String id;
    private String name;
}


When using this class with two resource classes which simply return either one or two Customer's wrapped in a Response object, everything is serialized correctly, in both XML and JSON. When a list of two Customer's is returned, the list is wrapped in GenericEntity<List<Customer>>, and all works as expected, for both formats.

I then try to externalize the JAXB mappings to an OXM file which looks like this:
<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
              xmlns:xsi="..."
              xsi:schemaLocation="http://www.eclipse.org/eclipselink/xsds/persistence/oxm http://www.eclipse.org/eclipselink/xsds/eclipselink_oxm_2_4.xsd"
              package-name="{package}"
              xml-accessor-type="FIELD"
              version="2.4">

    <java-types>
        <java-type name="Customer">
            <xml-root-element/>
            <java-attributes>
                <xml-element java-attribute="id" name="customer-id"/>
                <xml-element java-attribute="name" name="customer-name"/>
            </java-attributes>
        </java-type>
    </java-types>

</xml-bindings>


I add a jaxb.properties file to the package containing Customer, and the following context resolver:

@Provider
public class AppContextResolver implements ContextResolver<JAXBContext> {

    public AppContextResolver() {

        types = new Class<?>[]{Customer.class};

        try {

            Map<String, Object> props = new HashMap<>(1);
            props.put(JAXBContextProperties.OXM_METADATA_SOURCE, "{package}/xml-bindings.xml");

            context = JAXBContext.newInstance(types, props);

        } catch (JAXBException ex) {

            throw new RuntimeException(ex);
        }
    }

    @Override
    public JAXBContext getContext(Class<?> type) {

        return Arrays.asList(types).contains(type) ? context : null;
    }

    private final Class<?>[] types;
    private final JAXBContext context;
}


There is a minor irritant in that I need to keep the @XmlRootElement annotation on the Customer class; I don't know what the <xml-root-element/> element is supposed to accomplish, but that's a minor problem. The real problem is:

When returning a single Customer object wrapped in a Response, everything is serialized correctly, in both XML and JSON, and I know the bindings are being used because the attributes have the names which only appear in the mapping file (customer-id and customer-name).

However, when returning a list of two Customer's wrapped in a GenericEntity, the XML is still correct, but the JSON is empty. I get [{},{}].

The marshaller knows there are two objects, but produces no content. I want to reiterate that if I simply remove jaxb.properties and the custom context provider, Jersey handles the GenericEntity list correctly for both XML and JSON, but switching to the custom provider, it simply writes out no content for JSON.

Ultimately, I want to use different OXM files in my own JAX-RS resources, and the API which exposes my model in JPARS. The JPARS oxm file is working fine, including collections, in JSON and XML, even allowing me to remove the @XmlRootElement from the entities.

I assume there is something simple in setting up the bootstrapping which I am missing, but I can't figure it out.

Dave
Previous Topic:JMS Cache Coordination: setDeliveryMode to be non persistent on the TopicPublisher
Next Topic:Using JPA-RS in Equinox (via gemini)
Goto Forum:
  


Current Time: Thu Oct 10 23:12:10 GMT 2024

Powered by FUDForum. Page generated in 0.03174 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top