Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "EclipseLink/Examples/MOXy/MOXy JSON Provider"

(New page: = MOXy JSON Provider = MOXy now includes an implementation (MOXyJsonProvider) that can be used directly or extended to make the integration even easier. == Using MOXyJsonProvider == ===...)
 
 
(12 intermediate revisions by the same user not shown)
Line 1: Line 1:
= MOXy JSON Provider =
+
<div style="margin:5px;float:right;border:1px solid #000000;padding:5px">__TOC__</div>
  
MOXy now includes an implementation (MOXyJsonProvider) that can be used directly or extended to make the integration even easier.
+
=MOXy JSON Provider=
  
== Using MOXyJsonProvider ==
+
MOXy now includes an implementation of MessageBodyReader / MessageBodyWriter, called '''MOXyJsonProvider''', that can be used directly or extended to make JAX-RS integration even easier.
  
=== Default Behaviour ===
+
 
 +
==Default Behaviour==
  
 
You can use a JAX-RS Application class to specify that '''MOXyJsonProvider''' should be used with your JAX-RS application.
 
You can use a JAX-RS Application class to specify that '''MOXyJsonProvider''' should be used with your JAX-RS application.
Line 17: Line 18:
 
import org.eclipse.persistence.jaxb.rs.MOXyJsonProvider;
 
import org.eclipse.persistence.jaxb.rs.MOXyJsonProvider;
 
   
 
   
public class CustomerApplication extends Application {
+
public class CustomerApplication extends Application {
 
   
 
   
 
     @Override
 
     @Override
Line 25: Line 26:
 
         set.add(CustomerService.class);
 
         set.add(CustomerService.class);
 
         return set;
 
         return set;
 +
    }
 +
 +
}
 +
</source>
 +
</div>
 +
 +
 +
==Configuration Options==
 +
 
 +
You can also use a JAX-RS Application class to specify an instance of MOXyJsonProvider to be used with your JAX-RS application.  This approach allows you customize the different configuration options offered by the MOXyJsonProvider.
 +
 +
<div style="width:850px">
 +
<source lang="java">
 +
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.setAttributePrefix("@");
 +
        moxyJsonProvider.setFormattedOutput(true);
 +
        moxyJsonProvider.setIncludeRoot(true);
 +
        moxyJsonProvider.setMarshalEmptyCollections(false);
 +
        moxyJsonProvider.setValueWrapper("$");
 +
 +
        Map<String, String> namespacePrefixMapper = new HashMap<String, String>(1);
 +
        namespacePrefixMapper.put("http://www.example.org/customer", "cust");
 +
        moxyJsonProvider.setNamespacePrefixMapper(namespacePrefixMapper);
 +
        moxyJsonProvider.setNamespaceSeparator(':');
 +
 +
        HashSet<Object> set = new HashSet<Object>(1);
 +
        set.add(moxyJsonProvider);
 +
        return set;
 +
    }
 +
 +
}
 +
</source>
 +
</div>
 +
 +
 +
==WEB-INF/web.xml==
 +
 +
A web.xml file is used to configure the Application class:
 +
 +
<div style="width:850px">
 +
<source lang="xml">
 +
<?xml version="1.0" encoding="UTF-8"?>
 +
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
 +
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 +
    <servlet>
 +
        <servlet-name>Jersey Web Application</servlet-name>
 +
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer
 +
        </servlet-class>
 +
        <load-on-startup>1</load-on-startup>
 +
        <init-param>
 +
            <param-name>javax.ws.rs.Application</param-name>
 +
            <param-value>org.example.CustomerApplication</param-value>
 +
        </init-param>
 +
    </servlet>
 +
    <servlet-mapping>
 +
        <servlet-name>Jersey Web Application</servlet-name>
 +
        <url-pattern>/rest/*</url-pattern>
 +
    </servlet-mapping>
 +
    <persistence-context-ref>
 +
        <persistence-context-ref-name>persistence/em</persistence-context-ref-name>
 +
        <persistence-unit-name>CustomerService</persistence-unit-name>
 +
    </persistence-context-ref>
 +
</web-app>
 +
</source>
 +
</div>
 +
 +
 +
==Extending MOXyJsonProvider==
 +
 
 +
You can also extend MOXyJsonProvider to create your own '''MessageBodyReader/MessageBodyWriter'''.  The <tt>preReadFrom</tt> and <tt>preWriteTo</tt> methods can be overridden to customize the Unmarshaller/Marshaller that will be used by MOXy.
 +
 +
<div style="width:850px">
 +
<source lang="java">
 +
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, "$");
 
     }
 
     }
 
   
 
   

Latest revision as of 14:17, 18 June 2012

MOXy JSON Provider

MOXy now includes an implementation of MessageBodyReader / MessageBodyWriter, called MOXyJsonProvider, that can be used directly or extended to make JAX-RS integration even easier.


Default Behaviour

You can use a JAX-RS 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 CustomerApplication extends Application {
 
    @Override
    public Set<Class<?>> getClasses() {
        HashSet<Class<?>> set = new HashSet<Class<?>>(2);
        set.add(MOXyJsonProvider.class);
        set.add(CustomerService.class);
        return set;
    }
 
}


Configuration Options

You can also use a JAX-RS Application class to specify an instance of MOXyJsonProvider to be used with your JAX-RS application. This approach allows you customize the different configuration options offered by the MOXyJsonProvider.

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.setAttributePrefix("@");
        moxyJsonProvider.setFormattedOutput(true);
        moxyJsonProvider.setIncludeRoot(true);
        moxyJsonProvider.setMarshalEmptyCollections(false);
        moxyJsonProvider.setValueWrapper("$");
 
        Map<String, String> namespacePrefixMapper = new HashMap<String, String>(1);
        namespacePrefixMapper.put("http://www.example.org/customer", "cust");
        moxyJsonProvider.setNamespacePrefixMapper(namespacePrefixMapper);
        moxyJsonProvider.setNamespaceSeparator(':');
 
        HashSet<Object> set = new HashSet<Object>(1);
        set.add(moxyJsonProvider);
        return set;
    }
 
}


WEB-INF/web.xml

A web.xml file is used to configure the Application class:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer
        </servlet-class>
        <load-on-startup>1</load-on-startup>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>org.example.CustomerApplication</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
    <persistence-context-ref>
        <persistence-context-ref-name>persistence/em</persistence-context-ref-name>
        <persistence-unit-name>CustomerService</persistence-unit-name>
    </persistence-context-ref>
</web-app>


Extending MOXyJsonProvider

You can also extend MOXyJsonProvider to create your own MessageBodyReader/MessageBodyWriter. The preReadFrom and preWriteTo methods can be overridden to customize the Unmarshaller/Marshaller that will be used by MOXy.

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, "$");
    }
 
}

Back to the top