Note. I am an EclipseLink JAXB (MOXy) and a member of the JAXB Group (JSR-222) .
You can use the JSON binding offered by MOXy for this use case.
Domain Model (root)
import javax.xml.bind.annotation.*; @XmlAccessorType(XmlAccessType.FIELD) public class Root { int number; String string; }
Specifying MOXy as a JSON Binding Provider
In a RESTful environment, you can specify MOXyJsonProvider as MessageBodyReader / MessageBodyWriter for your JAX-RS application
In a separate example below, you can specify the jaxb.properties file in the same package as your domain model, with the following entry (see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as -your.html ):
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
Demo code
The following is a separate example that you can run to prove that everything works:
import java.util.*; import javax.xml.bind.*; 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[] {Root.class}, properties); Root root = new Root(); root.number = 1234; root.string = "1234"; Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(root, System.out); } }
Output
The following is the result of running the demo code:
{ "number" : 1234, "string" : "1234" }
source share