|
EclipseLink 2.4.2, build 'v20130514-5956486' API Reference | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.eclipse.persistence.jaxb.rs.MOXyJsonProvider
public class MOXyJsonProvider
This is an implementation of MessageBodyReader/MessageBodyWriter that can be used to enable EclipseLink JAXB (MOXy) as the the JSON provider.
Supported Media Type Patterns
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 MOXyJsonProvider
You 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 MOXyJsonProvider
You 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, MultivaluedMaphttpHeaders, 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, "$"); } }
Field Summary | |
---|---|
protected javax.ws.rs.ext.Providers |
providers
|
Constructor Summary | |
---|---|
MOXyJsonProvider()
|
Method Summary | |
---|---|
java.lang.String |
getAttributePrefix()
The value that will be prepended to all keys that are mapped to an XML attribute. |
protected java.lang.Class<?> |
getDomainClass(java.lang.reflect.Type genericType)
A convenience method to get the domain class (i.e. |
protected JAXBContext |
getJAXBContext(java.lang.Class<?> domainClass,
java.lang.annotation.Annotation[] annotations,
javax.ws.rs.core.MediaType mediaType,
javax.ws.rs.core.MultivaluedMap<java.lang.String,?> httpHeaders)
Return the JAXBContext that corresponds to the domain class. |
java.util.Map<java.lang.String,java.lang.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(java.lang.Object t,
java.lang.Class<?> type,
java.lang.reflect.Type genericType,
java.lang.annotation.Annotation[] annotations,
javax.ws.rs.core.MediaType mediaType)
|
java.lang.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(java.lang.Class<?> type,
java.lang.reflect.Type genericType,
java.lang.annotation.Annotation[] annotations,
javax.ws.rs.core.MediaType mediaType)
|
boolean |
isWrapperAsArrayName()
If true the grouping element will be used as the JSON key. |
boolean |
isWriteable(java.lang.Class<?> type,
java.lang.reflect.Type genericType,
java.lang.annotation.Annotation[] annotations,
javax.ws.rs.core.MediaType mediaType)
|
protected void |
preReadFrom(java.lang.Class<java.lang.Object> type,
java.lang.reflect.Type genericType,
java.lang.annotation.Annotation[] annotations,
javax.ws.rs.core.MediaType mediaType,
javax.ws.rs.core.MultivaluedMap<java.lang.String,java.lang.String> httpHeaders,
Unmarshaller unmarshaller)
Subclasses of MOXyJsonProvider can override this method to customize the instance of Unmarshaller that will be used to unmarshal the JSON message in the readFrom call. |
protected void |
preWriteTo(java.lang.Object object,
java.lang.Class<?> type,
java.lang.reflect.Type genericType,
java.lang.annotation.Annotation[] annotations,
javax.ws.rs.core.MediaType mediaType,
javax.ws.rs.core.MultivaluedMap<java.lang.String,java.lang.Object> httpHeaders,
Marshaller marshaller)
Subclasses of MOXyJsonProvider can override this method to customize the instance of Marshaller that will be used to marshal the domain objects to JSON in the writeTo call. |
java.lang.Object |
readFrom(java.lang.Class<java.lang.Object> type,
java.lang.reflect.Type genericType,
java.lang.annotation.Annotation[] annotations,
javax.ws.rs.core.MediaType mediaType,
javax.ws.rs.core.MultivaluedMap<java.lang.String,java.lang.String> httpHeaders,
java.io.InputStream entityStream)
|
void |
setAttributePrefix(java.lang.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(java.util.Map<java.lang.String,java.lang.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(java.lang.String valueWrapper)
Specify the key that will correspond to the property mapped with |
void |
setWrapperAsArrayName(boolean wrapperAsArrayName)
If true the grouping element will be used as the JSON key. |
protected boolean |
supportsMediaType(javax.ws.rs.core.MediaType mediaType)
|
void |
writeTo(java.lang.Object object,
java.lang.Class<?> type,
java.lang.reflect.Type genericType,
java.lang.annotation.Annotation[] annotations,
javax.ws.rs.core.MediaType mediaType,
javax.ws.rs.core.MultivaluedMap<java.lang.String,java.lang.Object> httpHeaders,
java.io.OutputStream entityStream)
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
@Context protected javax.ws.rs.ext.Providers providers
Constructor Detail |
---|
public MOXyJsonProvider()
Method Detail |
---|
public java.lang.String getAttributePrefix()
org.eclipse.persistence.jaxb.MarshallerPropertes.JSON_ATTRIBUTE_PREFIX
,
org.eclipse.persistence.jaxb.UnmarshallerPropertes.JSON_ATTRIBUTE_PREFIX
protected java.lang.Class<?> getDomainClass(java.lang.reflect.Type genericType)
genericType
- - The parameter/return type of the JAX-RS operation.
protected JAXBContext getJAXBContext(java.lang.Class<?> domainClass, java.lang.annotation.Annotation[] annotations, javax.ws.rs.core.MediaType mediaType, javax.ws.rs.core.MultivaluedMap<java.lang.String,?> httpHeaders) throws JAXBException
domainClass
- - The domain class we need a JAXBContext for.annotations
- - The annotations corresponding to domain object.mediaType
- - The media type for the HTTP entity.httpHeaders
- - HTTP headers associated with HTTP entity.
JAXBException
public java.util.Map<java.lang.String,java.lang.String> getNamespacePrefixMapper()
org.eclipse.persistence.jaxb.MarshallerProperties.NAMESPACE_PREFIX_MAPPER
,
org.eclipse.persistence.jaxb.UnmarshallerProperties.JSON_NAMESPACE_PREFIX_MAPPER
public char getNamespaceSeparator()
org.eclipse.persistence.jaxb.MarshallerProperties.NAMESPACE_SEPARATOR
,
org.eclipse.persistence.jaxb.UnmarshallerProperties.NAMESPACE_SEPARATOR
public long getSize(java.lang.Object t, java.lang.Class<?> type, java.lang.reflect.Type genericType, java.lang.annotation.Annotation[] annotations, javax.ws.rs.core.MediaType mediaType)
getSize
in interface javax.ws.rs.ext.MessageBodyWriter<java.lang.Object>
public java.lang.String getValueWrapper()
org.eclipse.persistence.jaxb.MarshallerPropertes.JSON_VALUE_WRAPPER
,
org.eclipse.persistence.jaxb.UnmarshallerPropertes.JSON_VALUE_WRAPPER
public boolean isFormattedOutput()
public boolean isIncludeRoot()
org.eclipse.persistence.jaxb.MarshallerPropertes.JSON_INCLUDE_ROOT
,
org.eclipse.persistence.jaxb.UnmarshallerPropertes.JSON_INCLUDE_ROOT
public boolean isMarshalEmptyCollections()
org.eclipse.persistence.jaxb.MarshallerProperties.JSON_MARSHAL_EMPTY_COLLECTIONS
public boolean isReadable(java.lang.Class<?> type, java.lang.reflect.Type genericType, java.lang.annotation.Annotation[] annotations, javax.ws.rs.core.MediaType mediaType)
isReadable
in interface javax.ws.rs.ext.MessageBodyReader<java.lang.Object>
public boolean isWrapperAsArrayName()
Example
Given the following class:
@XmlAccessorType(XmlAccessType.FIELD) public class Customer { @XmlElementWrapper(name="phone-numbers") @XmlElement(name="phone-number") private ListphoneNumbers; }
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" : [ { ... }, { ... }] }
org.eclipse.persistence.jaxb.JAXBContextProperties.JSON_WRAPPER_AS_ARRAY_NAME
,
org.eclipse.persistence.jaxb.MarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME
,
org.eclipse.persistence.jaxb.UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME
public boolean isWriteable(java.lang.Class<?> type, java.lang.reflect.Type genericType, java.lang.annotation.Annotation[] annotations, javax.ws.rs.core.MediaType mediaType)
isWriteable
in interface javax.ws.rs.ext.MessageBodyWriter<java.lang.Object>
protected void preReadFrom(java.lang.Class<java.lang.Object> type, java.lang.reflect.Type genericType, java.lang.annotation.Annotation[] annotations, javax.ws.rs.core.MediaType mediaType, javax.ws.rs.core.MultivaluedMap<java.lang.String,java.lang.String> httpHeaders, Unmarshaller unmarshaller) throws JAXBException
type
- - The Class to be unmarshalled (i.e. Customer or
List)genericType
- - The type of object to be unmarshalled (i.e
Customer or List<Customer>).annotations
- - The annotations corresponding to domain object.mediaType
- - The media type for the HTTP entity.httpHeaders
- - HTTP headers associated with HTTP entity.unmarshaller
- - The instance of Unmarshaller that will be
used to unmarshal the JSON message.
JAXBException
UnmarshallerProperties
protected void preWriteTo(java.lang.Object object, java.lang.Class<?> type, java.lang.reflect.Type genericType, java.lang.annotation.Annotation[] annotations, javax.ws.rs.core.MediaType mediaType, javax.ws.rs.core.MultivaluedMap<java.lang.String,java.lang.Object> httpHeaders, Marshaller marshaller) throws JAXBException
object
- - The domain object that will be marshalled to JSON.type
- - The Class to be marshalled (i.e. Customer or
List)genericType
- - The type of object to be marshalled (i.e
Customer or List<Customer>).annotations
- - The annotations corresponding to domain object.mediaType
- - The media type for the HTTP entity.httpHeaders
- - HTTP headers associated with HTTP entity.marshaller
- - The instance of Marshaller that will be used
to marshal the domain object to JSON.
JAXBException
MarshallerProperties
public java.lang.Object readFrom(java.lang.Class<java.lang.Object> type, java.lang.reflect.Type genericType, java.lang.annotation.Annotation[] annotations, javax.ws.rs.core.MediaType mediaType, javax.ws.rs.core.MultivaluedMap<java.lang.String,java.lang.String> httpHeaders, java.io.InputStream entityStream) throws java.io.IOException, javax.ws.rs.WebApplicationException
readFrom
in interface javax.ws.rs.ext.MessageBodyReader<java.lang.Object>
java.io.IOException
javax.ws.rs.WebApplicationException
public void setAttributePrefix(java.lang.String attributePrefix)
org.eclipse.persistence.jaxb.MarshallerPropertes.JSON_ATTRIBUTE_PREFIX
,
org.eclipse.persistence.jaxb.UnmarshallerPropertes.JSON_ATTRIBUTE_PREFIX
public void setFormattedOutput(boolean formattedOutput)
formattedOutput
- - true if the output should be formatted, else
false.public void setIncludeRoot(boolean includeRoot)
includeRoot
- - true if the message includes the root node, else
false.org.eclipse.persistence.jaxb.MarshallerPropertes.JSON_INCLUDE_ROOT
,
org.eclipse.persistence.jaxb.UnmarshallerPropertes.JSON_INCLUDE_ROOT
public void setMarshalEmptyCollections(boolean marshalEmptyCollections)
org.eclipse.persistence.jaxb.MarshallerProperties.JSON_MARSHAL_EMPTY_COLLECTIONS
public void setNamespacePrefixMapper(java.util.Map<java.lang.String,java.lang.String> namespacePrefixMapper)
org.eclipse.persistence.jaxb.MarshallerProperties.NAMESPACE_PREFIX_MAPPER
,
org.eclipse.persistence.jaxb.UnmarshallerProperties.JSON_NAMESPACE_PREFIX_MAPPER
public void setNamespaceSeparator(char namespaceSeparator)
org.eclipse.persistence.jaxb.MarshallerProperties.NAMESPACE_SEPARATOR
,
org.eclipse.persistence.jaxb.UnmarshallerProperties.NAMESPACE_SEPARATOR
public void setWrapperAsArrayName(boolean wrapperAsArrayName)
Example
Given the following class:
@XmlAccessorType(XmlAccessType.FIELD) public class Customer { @XmlElementWrapper(name="phone-numbers") @XmlElement(name="phone-number") private ListphoneNumbers; }
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" : [ { ... }, { ... }] }
org.eclipse.persistence.jaxb.JAXBContextProperties.JSON_WRAPPER_AS_ARRAY_NAME
,
org.eclipse.persistence.jaxb.MarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME
,
org.eclipse.persistence.jaxb.UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME
public void setValueWrapper(java.lang.String valueWrapper)
org.eclipse.persistence.jaxb.MarshallerPropertes.JSON_VALUE_WRAPPER
,
org.eclipse.persistence.jaxb.UnmarshallerPropertes.JSON_VALUE_WRAPPER
protected boolean supportsMediaType(javax.ws.rs.core.MediaType mediaType)
public void writeTo(java.lang.Object object, java.lang.Class<?> type, java.lang.reflect.Type genericType, java.lang.annotation.Annotation[] annotations, javax.ws.rs.core.MediaType mediaType, javax.ws.rs.core.MultivaluedMap<java.lang.String,java.lang.Object> httpHeaders, java.io.OutputStream entityStream) throws java.io.IOException, javax.ws.rs.WebApplicationException
writeTo
in interface javax.ws.rs.ext.MessageBodyWriter<java.lang.Object>
java.io.IOException
javax.ws.rs.WebApplicationException
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)
|
EclipseLink 2.4.2, build 'v20130514-5956486' API Reference | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |