Class MOXyJsonProvider

java.lang.Object
org.eclipse.persistence.jaxb.rs.MOXyJsonProvider
All Implemented Interfaces:
MessageBodyReader<Object>, MessageBodyWriter<Object>

@Produces({"application/json","*/*","application/x-javascript"}) @Consumes({"application/json","*/*"}) @Provider public class MOXyJsonProvider extends Object implements MessageBodyReader<Object>, 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 Behavior

You can use the Application class to specify that MOXyJsonProvider should be used with your JAX-RS application.

 package org.example;

 import java.util.*;
 import jakarta.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 jakarta.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 jakarta.ws.rs.*;
 import jakarta.ws.rs.core.*;
 import jakarta.ws.rs.ext.Provider;
 import jakarta.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