JAXB Mapping to JSON

后端 未结 2 1860
傲寒
傲寒 2020-12-06 01:28

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

相关标签:
2条回答
  • 2020-12-06 02:12

    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:

    • http://blog.bdoughan.com/2012/05/moxy-as-your-jax-rs-json-provider.html
    • http://blog.bdoughan.com/2013/06/moxy-is-new-default-json-binding.html
    0 讨论(0)
  • 2020-12-06 02:13

    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

    0 讨论(0)
提交回复
热议问题