JAX-RS

org.apache.johnzon:johnzon-jaxrs:2.2.0

Contents

johnzon-jaxrs provides JAX-RS providers with the underlying MessageBodyReaders and MessageBodyWriters:

  • org.apache.johnzon.jaxrs.[Wildcard]JohnzonProvider: maps objects to JSON and back with the Johnzon mapper
  • org.apache.johnzon.jaxrs.[Wildcard]ConfigurableJohnzonProvider: the same, plus setters to ease configuration in most servers and containers
  • org.apache.johnzon.jaxrs.[Wildcard]JsrProvider: works with JsrArray, JsrObject and more generally JsonStructure

The Wildcard variants support */json, */*+json, */x-json, */javascript and */x-javascript instead of only application/json. This makes it easier to mix JSON with other media types in the same resource (text/plain, XML, …), since some JAX-RS API versions match as a true wildcard type whatever the subtype is.

ConfigurableJohnzonProvider exposes most of the MapperBuilder configuration as setters, so any IoC container can configure the provider, including containers not based on a programming language.

When used with johnzon-core, these providers do not throw NoContentException on an empty incoming stream (except JsrProvider) to limit breaking changes.

A JSON-B based provider, JsonbJaxrsProvider, ships with the JSON-B module.

TomEE configuration

TomEE 7.x and later use Johnzon as their default JAX-RS provider. To customize the provider, first declare it in WEB-INF/openejb-jar.xml:

<?xml version="1.0" encoding="UTF-8"?>
<openejb-jar>
  <pojo-deployment class-name="jaxrs-application">
    <properties>
      # optional but requires to skip scanned providers if set to true
      cxf.jaxrs.skip-provider-scanning = true
      # list of providers we want
      cxf.jaxrs.providers = johnzon,org.apache.openejb.server.cxf.rs.EJBAccessExceptionMapper
    </properties>
  </pojo-deployment>
</openejb-jar>

Then define the johnzon service instantiating the provider in WEB-INF/resources.xml:

<?xml version="1.0" encoding="UTF-8"?>
<resources>
  <Service id="johnzon" class-name="org.apache.johnzon.jaxrs.ConfigurableJohnzonProvider">
    # 1M
    maxSize = 1048576
    bufferSize = 1048576

    # ordered attributes
    attributeOrder = $order

    # Additional types to ignore
    ignores = org.apache.cxf.jaxrs.ext.multipart.MultipartBody
  </Service>

  <Service id="order" class-name="com.company.MyAttributeSorter" />

</resources>

The service id johnzon matches the name referenced in openejb-jar.xml; other instances can be referenced with $id for services and @id for resources.