14

I have written a JAX-RS (Jersey) REST Service, which accepts XML messages of ONIX XML format. Generally, I have generated all the required classes for JAXB binding from the given schema with xjc. There are more than 500 classes overall and I cannot modify them.

Now, when I have a JAXB-mapped object, I need to store it to the database. I work with mongoDb, so the message format should be JSON. I tried to use Jackson with JAXB module to convert JAXB-object into JSON, which works pretty fine with storing the data. But when I try to convert the JSON back into the JAXB object, it throws an exception connected somehow with the JAXBElement. In google I found out that the JAXBElement is not supported in Jackson and I have to work around this issue. But I cant do it because I cannot modify JAXB-generated classes.

Is there a way to map JAXB Objects into JSON with some other means, but which will follow the whole JAXB specification so that I have no problems in the future converting from JSON to the JAXB object and visa vera?

Artem Moskalev
  • 5,748
  • 11
  • 36
  • 55
  • 1
    Jackson should support that annotation according to [Using JAXB annotations with Jackson](http://wiki.fasterxml.com/JacksonJAXBAnnotations). – Prmths Oct 03 '13 at 20:00
  • 1
    You could also just use straight JAXB. There is a good article about that here: [Binding to JSON & XML – Handling Collections](http://www.javacodegeeks.com/2013/03/binding-to-json-xml-handling-collections.html) – Prmths Oct 03 '13 at 20:06
  • 2
    @Prmths - I have added an answer expanding on your comment: http://stackoverflow.com/a/19168878/383861 – bdoughan Oct 03 '13 at 20:43
  • @BlaiseDoughan, thanks for the reference. I pretty much look for anything you've written when dealing with MOXy or JAXB. – Prmths Oct 03 '13 at 20:58
  • Take a look at [this question](http://stackoverflow.com/questions/26063268/create-marshaller-for-json-from-unknown-jaxbcontext-xml) if you found yourself here in this old question. – Sebastian Barth Sep 30 '14 at 17:12

2 Answers2

18

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

If you can't do it with Jackson, you use case will work with MOXy.

Java Model

Foo

Here is a sample class that contains a field of type JAXBElement.

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Foo {

    @XmlElementRef(name="bar")
    private JAXBElement<Bar> bar;

}

Bar

public class Bar {

}

ObjectFactory

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;
import javax.xml.namespace.QName;

@XmlRegistry
public class ObjectFactory {
    
    @XmlElementDecl(name = "bar")
    public JAXBElement<Bar> createBar(Bar bar) {
        return new JAXBElement<Bar>(new QName("bar"), Bar.class, bar);
    }

}

Standalone Demo Code

Below is some demo code you can run in Java SE to see that everything works:

Demo

import java.util.*;
import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;

import org.eclipse.persistence.jaxb.JAXBContextProperties;

public class Demo {

    public static void main(String[] args) throws Exception {
        Map<String, Object> properties = new HashMap<String, Object>(2);
        properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
        properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
        JAXBContext jc = JAXBContext.newInstance(new Class[] {Foo.class, ObjectFactory.class}, properties);
        
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        StreamSource json = new StreamSource("src/forum19158056/input.json");
        Foo foo = unmarshaller.unmarshal(json, Foo.class).getValue();
        
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(foo, System.out);
    }

}

input.json/Output

{"bar" : {} }

Running with JAX-RS

The following links will help you leverage MOXy in a JAX-RS service:

Roland
  • 22,259
  • 4
  • 57
  • 84
bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • 1
    I managed to do it with MOXy. Works fine, but I have to do it in this way inside the resource method: JAXBContext context = JAXBContextFactory.createContext(new Class[] {ONIXMessage.class}, null); How can I fetch the eclipselink JAXBContext from Jersey? – Artem Moskalev Oct 04 '13 at 09:41
  • 2
    are there non-eclipselink (i.e. standard) properties for setting up the JAXBContext in JavaEE 7? – Archimedes Trajano Jun 25 '14 at 15:53
9

Since you're using Jackson you can construct an ObjectMapper with the JaxbAnnotationModule and write out the value. The following is some code to write a JAXB annotated object to system out.

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JaxbAnnotationModule());
        mapper.writeValue(System.out, jaxbObject);

This approach will also work on Glassfish as it does not utilize the provider given by the container which would cause ClassNotFoundException as per GLASSFISH-21141

Archimedes Trajano
  • 35,625
  • 19
  • 175
  • 265