Use the eclipselink.media-type property on your JAXB Marshaller or Unmarsaller to produce and use JSON documents with your application, as shown in Example 10-1.
Example 10-1 Marshalling and Unmarshalling
... Marshaller m = jaxbContext.createMarshaller(); m.setProperty("eclipselink.media-type", "application/json"); Unmarshaller u = jaxbContext.createUnmarshaller(); u.setProperty("eclipselink.media-type", "application/json"); ...
You can also specify the eclipselink.media-type
property in the Map
of the properties used when you create the JAXBContext
, as shown in Example 10-2.
Example 10-2 Using a Map
import org.eclipse.persistence.jaxb.JAXBContextProperties; import org.eclipse.persistence.oxm.MediaType; Map<String, Object> properties = new HashMap<String, Object>(); properties.put("eclipselink.media-type", "application/json"); JAXBContext ctx = JAXBContext.newInstance(new Class[] { Employee.class }, properties); Marshaller jsonMarshaller = ctx.createMarshaller(); Unmarshaller jsonUnmarshaller = ctx.createUnmarshaller();
When specified in a Map, the Marshallers and Unmarshallers created from the JAXBContent
will automatically use the specified media type.
You can also configure your application to use JSON documents by using the MarshallerProperties
, UnmarshallerProperties
, and MediaType
constants, as shown in Example 10-3.
Example 10-3 Using MarshallerProperties and UnarshallerProperties
import org.eclipse.persistence.jaxb.MarshallerProperties; import org.eclipse.persistence.jaxb.UnarshallerProperties; import org.eclipse.persistence.oxm.MediaType; m.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON); u.setProperty(UnmarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON); ...