Class MOXyJsonProvider
- java.lang.Object
-
- org.eclipse.persistence.jaxb.rs.MOXyJsonProvider
-
- All Implemented Interfaces:
javax.ws.rs.ext.MessageBodyReader<Object>
,javax.ws.rs.ext.MessageBodyWriter<Object>
@Produces({"application/json","*/*","application/x-javascript"}) @Consumes({"application/json","*/*"}) public class MOXyJsonProvider extends Object implements javax.ws.rs.ext.MessageBodyReader<Object>, javax.ws.rs.ext.MessageBodyWriter<Object>
This is an implementation of MessageBodyReader/MessageBodyWriter that can be used to enable EclipseLink JAXB (MOXy) as the JSON provider.
Supported Media Type Patterns
- */json (i.e. application/json and text/json)
- */*+json
Below are some different usage options.
Option #1 - MOXyJsonProvider Default BehaviorYou can use the Application class to specify that MOXyJsonProvider should be used with your JAX-RS application.
package org.example; import java.util.*; import javax.ws.rs.core.Application; import org.eclipse.persistence.jaxb.rs.MOXyJsonProvider; public class ExampleApplication extends Application { @Override public Set<Class<?>> getClasses() { HashSet<Class<?>> set = new HashSet<Class<?>>(2); set.add(MOXyJsonProvider.class); set.add(ExampleService.class); return set; } }
Option #2 - Customize MOXyJsonProviderYou can use the Application class to specify a configured instance of MOXyJsonProvider should be used with your JAX-RS application.
package org.example; import java.util.*; import javax.ws.rs.core.Application; import org.eclipse.persistence.jaxb.rs.MOXyJsonProvider; public class CustomerApplication extends Application { @Override public Set<Class<?>> getClasses() { HashSet<Class<?>> set = new HashSet<Class<?>>(1); set.add(ExampleService.class); return set; } @Override public Set<Object> getSingletons() { moxyJsonProvider moxyJsonProvider = new MOXyJsonProvider(); moxyJsonProvider.setFormattedOutput(true); moxyJsonProvider.setIncludeRoot(true); HashSet<Object> set = new HashSet<Object>(2); set.add(moxyJsonProvider); return set; } }
Option #3 - Extend MOXyJsonProviderYou can use MOXyJsonProvider for creating your own MessageBodyReader/MessageBodyWriter.
package org.example; import java.lang.annotation.Annotation; import java.lang.reflect.Type; import javax.ws.rs.*; import javax.ws.rs.core.*; import javax.ws.rs.ext.Provider; import javax.xml.bind.*; import org.eclipse.persistence.jaxb.MarshallerProperties; import org.eclipse.persistence.jaxb.rs.MOXyJsonProvider; @Provider @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) public class CustomerJSONProvider extends MOXyJsonProvider { @Override public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { return getDomainClass(genericType) == Customer.class; } @Override public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { return isReadable(type, genericType, annotations, mediaType); } @Override protected void preReadFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, Unmarshaller unmarshaller) throws JAXBException { unmarshaller.setProperty(MarshallerProperties.JSON_VALUE_WRAPPER, "$"); } @Override protected void preWriteTo(Object object, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, Marshaller marshaller) throws JAXBException { marshaller.setProperty(MarshallerProperties.JSON_VALUE_WRAPPER, "$"); } }
- Since:
- 2.4
-
-
Constructor Summary
Constructors Constructor Description MOXyJsonProvider()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description String
getAttributePrefix()
The value that will be prepended to all keys that are mapped to an XML attribute.Class<?>
getDomainClass(Set<Class<?>> domainClasses)
Get first non java class if exists.Map<String,String>
getNamespacePrefixMapper()
By default the JSON-binding will ignore namespace qualification.char
getNamespaceSeparator()
This character (default is '.') separates the prefix from the key name.long
getSize(Object t, Class<?> type, Type genericType, Annotation[] annotations, javax.ws.rs.core.MediaType mediaType)
String
getValueWrapper()
The key that will correspond to the property mapped with @XmlValue.boolean
isFormattedOutput()
boolean
isIncludeRoot()
boolean
isMarshalEmptyCollections()
If true empty collections will be marshalled as empty arrays, else the collection will not be marshalled to JSON (default is true).boolean
isReadable(Class<?> type, Type genericType, Annotation[] annotations, javax.ws.rs.core.MediaType mediaType)
boolean
isWrapperAsArrayName()
If true the grouping element will be used as the JSON key.boolean
isWriteable(Class<?> type, Type genericType, Annotation[] annotations, javax.ws.rs.core.MediaType mediaType)
Object
readFrom(Class<Object> type, Type genericType, Annotation[] annotations, javax.ws.rs.core.MediaType mediaType, javax.ws.rs.core.MultivaluedMap<String,String> httpHeaders, InputStream entityStream)
void
setAttributePrefix(String attributePrefix)
Specify a value that will be prepended to all keys that are mapped to an XML attribute.void
setFormattedOutput(boolean formattedOutput)
Specify if the JSON output should be formatted (default is false).void
setIncludeRoot(boolean includeRoot)
Specify if the root node should be included in the JSON message (default is false).void
setMarshalEmptyCollections(boolean marshalEmptyCollections)
If true empty collections will be marshalled as empty arrays, else the collection will not be marshalled to JSON (default is true).void
setNamespacePrefixMapper(Map<String,String> namespacePrefixMapper)
By default the JSON-binding will ignore namespace qualification.void
setNamespaceSeparator(char namespaceSeparator)
This character (default is '.') separates the prefix from the key name.void
setValueWrapper(String valueWrapper)
Specify the key that will correspond to the property mapped with @XmlValue.void
setWrapperAsArrayName(boolean wrapperAsArrayName)
If true the grouping element will be used as the JSON key.void
writeTo(Object object, Class<?> type, Type genericType, Annotation[] annotations, javax.ws.rs.core.MediaType mediaType, javax.ws.rs.core.MultivaluedMap<String,Object> httpHeaders, OutputStream entityStream)
-
-
-
Method Detail
-
getAttributePrefix
public String getAttributePrefix()
The value that will be prepended to all keys that are mapped to an XML attribute. By default there is no attribute prefix.
-
getNamespacePrefixMapper
public Map<String,String> getNamespacePrefixMapper()
By default the JSON-binding will ignore namespace qualification. If this property is set the portion of the key before the namespace separator will be used to determine the namespace URI.
-
getNamespaceSeparator
public char getNamespaceSeparator()
This character (default is '.') separates the prefix from the key name. It is only used if namespace qualification has been enabled be setting a namespace prefix mapper.
-
getSize
public long getSize(Object t, Class<?> type, Type genericType, Annotation[] annotations, javax.ws.rs.core.MediaType mediaType)
- Specified by:
getSize
in interfacejavax.ws.rs.ext.MessageBodyWriter<Object>
-
getValueWrapper
public String getValueWrapper()
The key that will correspond to the property mapped with @XmlValue. This key will only be used if there are other mapped properties.
-
isFormattedOutput
public boolean isFormattedOutput()
- Returns:
- true if the JSON output should be formatted (default is false).
-
isIncludeRoot
public boolean isIncludeRoot()
- Returns:
- true if the root node is included in the JSON message (default is false).
- See Also:
MarshallerProperties.JSON_INCLUDE_ROOT
,UnmarshallerProperties.JSON_INCLUDE_ROOT
-
isMarshalEmptyCollections
public boolean isMarshalEmptyCollections()
If true empty collections will be marshalled as empty arrays, else the collection will not be marshalled to JSON (default is true).
-
isReadable
public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, javax.ws.rs.core.MediaType mediaType)
- Specified by:
isReadable
in interfacejavax.ws.rs.ext.MessageBodyReader<Object>
- Returns:
- true indicating that MOXyJsonProvider will
be used for the JSON binding if the media type is of the following
patterns */json or */*+json, and the type is not assignable from
any of (or a Collection or JAXBElement of) the following:
- byte[]
- java.io.File
- java.io.InputStream
- java.io.Reader
- java.lang.Object
- java.lang.String
- javax.activation.DataSource
-
isWrapperAsArrayName
public boolean isWrapperAsArrayName()
If true the grouping element will be used as the JSON key.Example
Given the following class:
@XmlAccessorType(XmlAccessType.FIELD) public class Customer { @XmlElementWrapper(name="phone-numbers") @XmlElement(name="phone-number") private List<PhoneNumber> phoneNumbers; }
If the property is set to false (the default) the JSON output will be:
{ "phone-numbers" : { "phone-number" : [ { ... }, { ... }] } }
And if the property is set to true, then the JSON output will be:
{ "phone-numbers" : [ { ... }, { ... }] }
-
isWriteable
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, javax.ws.rs.core.MediaType mediaType)
- Specified by:
isWriteable
in interfacejavax.ws.rs.ext.MessageBodyWriter<Object>
- Returns:
- true indicating that MOXyJsonProvider will
be used for the JSON binding if the media type is of the following
patterns */json or */*+json, and the type is not assignable from
any of (or a Collection or JAXBElement of) the following:
- byte[]
- java.io.File
- java.lang.Object
- java.lang.String
- javax.activation.DataSource
- javax.ws.rs.core.StreamingOutput
-
readFrom
public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, javax.ws.rs.core.MediaType mediaType, javax.ws.rs.core.MultivaluedMap<String,String> httpHeaders, InputStream entityStream) throws IOException, javax.ws.rs.WebApplicationException
- Specified by:
readFrom
in interfacejavax.ws.rs.ext.MessageBodyReader<Object>
- Throws:
IOException
javax.ws.rs.WebApplicationException
-
getDomainClass
public Class<?> getDomainClass(Set<Class<?>> domainClasses)
Get first non java class if exists.- Parameters:
domainClasses
-- Returns:
- first domain class or first generic class or just the first class from the list
-
setAttributePrefix
public void setAttributePrefix(String attributePrefix)
Specify a value that will be prepended to all keys that are mapped to an XML attribute. By default there is no attribute prefix.
-
setFormattedOutput
public void setFormattedOutput(boolean formattedOutput)
Specify if the JSON output should be formatted (default is false).- Parameters:
formattedOutput
- - true if the output should be formatted, else false.
-
setIncludeRoot
public void setIncludeRoot(boolean includeRoot)
Specify if the root node should be included in the JSON message (default is false).- Parameters:
includeRoot
- - true if the message includes the root node, else false.- See Also:
MarshallerProperties.JSON_INCLUDE_ROOT
,UnmarshallerProperties.JSON_INCLUDE_ROOT
-
setMarshalEmptyCollections
public void setMarshalEmptyCollections(boolean marshalEmptyCollections)
If true empty collections will be marshalled as empty arrays, else the collection will not be marshalled to JSON (default is true).
-
setNamespacePrefixMapper
public void setNamespacePrefixMapper(Map<String,String> namespacePrefixMapper)
By default the JSON-binding will ignore namespace qualification. If this property is set then a prefix corresponding to the namespace URI and a namespace separator will be prefixed to the key. include it you can specify a Map of namespace URI to prefix.
-
setNamespaceSeparator
public void setNamespaceSeparator(char namespaceSeparator)
This character (default is '.') separates the prefix from the key name. It is only used if namespace qualification has been enabled be setting a namespace prefix mapper.
-
setWrapperAsArrayName
public void setWrapperAsArrayName(boolean wrapperAsArrayName)
If true the grouping element will be used as the JSON key.Example
Given the following class:
@XmlAccessorType(XmlAccessType.FIELD) public class Customer { @XmlElementWrapper(name="phone-numbers") @XmlElement(name="phone-number") private List<PhoneNumber> phoneNumbers; }
If the property is set to false (the default) the JSON output will be:
{ "phone-numbers" : { "phone-number" : [ { ... }, { ... }] } }
And if the property is set to true, then the JSON output will be:
{ "phone-numbers" : [ { ... }, { ... }] }
-
setValueWrapper
public void setValueWrapper(String valueWrapper)
Specify the key that will correspond to the property mapped with @XmlValue. This key will only be used if there are other mapped properties.
-
writeTo
public void writeTo(Object object, Class<?> type, Type genericType, Annotation[] annotations, javax.ws.rs.core.MediaType mediaType, javax.ws.rs.core.MultivaluedMap<String,Object> httpHeaders, OutputStream entityStream) throws IOException, javax.ws.rs.WebApplicationException
- Specified by:
writeTo
in interfacejavax.ws.rs.ext.MessageBodyWriter<Object>
- Throws:
IOException
javax.ws.rs.WebApplicationException
- See Also:
MessageBodyWriter.writeTo(java.lang.Object, java.lang.Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType, javax.ws.rs.core.MultivaluedMap, java.io.OutputStream)
-
-