@Produces(value={"application/json","*/*","application/x-javascript"})
@Consumes(value={"application/json","*/*"})
@Provider
public class MOXyJsonProvider
extends java.lang.Object
implements javax.ws.rs.ext.MessageBodyReader<java.lang.Object>, javax.ws.rs.ext.MessageBodyWriter<java.lang.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
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,
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, "$");
}
}
| Modifier and Type | Field and Description |
|---|---|
protected javax.ws.rs.ext.Providers |
providers |
| Constructor and Description |
|---|
MOXyJsonProvider() |
| Modifier and Type | Method and Description |
|---|---|
java.lang.String |
getAttributePrefix()
The value that will be prepended to all keys that are mapped to an XML
attribute.
|
java.lang.Class<?> |
getDomainClass(java.util.Set<java.lang.Class<?>> domainClasses)
Get first non java class if exists.
|
protected java.util.Set<java.lang.Class<?>> |
getDomainClasses(java.lang.reflect.Type genericType)
A convenience method to get the domain class (i.e.
|
protected JAXBContext |
getJAXBContext(java.util.Set<java.lang.Class<?>> domainClasses,
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
@XmlValue.
|
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) |
public java.lang.String getAttributePrefix()
protected java.util.Set<java.lang.Class<?>> getDomainClasses(java.lang.reflect.Type genericType)
genericType - - The parameter/return type of the JAX-RS operation.protected JAXBContext getJAXBContext(java.util.Set<java.lang.Class<?>> domainClasses, java.lang.annotation.Annotation[] annotations, javax.ws.rs.core.MediaType mediaType, javax.ws.rs.core.MultivaluedMap<java.lang.String,?> httpHeaders) throws JAXBException
domainClasses - - The domain classes 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.JAXBExceptionpublic java.util.Map<java.lang.String,java.lang.String> getNamespacePrefixMapper()
public char getNamespaceSeparator()
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()
public boolean isFormattedOutput()
public boolean isIncludeRoot()
MarshallerProperties.JSON_INCLUDE_ROOT,
UnmarshallerProperties.JSON_INCLUDE_ROOTpublic boolean isMarshalEmptyCollections()
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 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" : [ {
...
}, {
...
}]
}
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.JAXBExceptionUnmarshallerPropertiesprotected 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.JAXBExceptionMarshallerPropertiespublic 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.IOExceptionjavax.ws.rs.WebApplicationExceptionpublic java.lang.Class<?> getDomainClass(java.util.Set<java.lang.Class<?>> domainClasses)
domainClasses - public void setAttributePrefix(java.lang.String attributePrefix)
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.MarshallerProperties.JSON_INCLUDE_ROOT,
UnmarshallerProperties.JSON_INCLUDE_ROOTpublic void setMarshalEmptyCollections(boolean marshalEmptyCollections)
public void setNamespacePrefixMapper(java.util.Map<java.lang.String,java.lang.String> namespacePrefixMapper)
public void setNamespaceSeparator(char namespaceSeparator)
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 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" : [ {
...
}, {
...
}]
}
public void setValueWrapper(java.lang.String valueWrapper)
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.IOExceptionjavax.ws.rs.WebApplicationExceptionMessageBodyWriter.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)