sebastiandaschner blog


Java collections as JAX-RS JSON response on TomEE

#jaxrs sunday, march 29, 2015

Since version 2 TomEE now uses Johnzon instead of Jettison. Although TomEE is not certified for JavaEE 7 yet, you can already try to run a EE 7 application with the latest TomEE builds. When returning a Java collection like LinkedList as JSON with JAX-RS, you get following error: No message body writer has been found for class java.util.LinkedList, ContentType: application/json.

To resolve this some properties have to be configured for the Johnzon serializer.

Add a file under src/main/webapp/WEB-INF/resources.xml with following content to configure the ConfigurableJohnzonProvider.

<?xml version="1.0"?>
<resources>
    <Service id="johnzon" class-name="org.apache.johnzon.jaxrs.ConfigurableJohnzonProvider">
        supportHiddenAccess = true
        doCloseOnStreams = false
        version = 2
        skipNull = false
        skipEmptyArray = false
    </Service>
</resources>

To tell CXF to take the configured service as JAX-RS provider, add another file src/main/webapp/WEB-INF/openejb-jar.xml:

<openejb-jar xmlns="http://www.openejb.org/openejb-jar/1.1"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.openejb.org/openejb-jar/1.1">
    <pojo-deployment class-name="jaxrs-application">
        <properties>
            cxf.jaxrs.providers = johnzon
        </properties>
    </pojo-deployment>
</openejb-jar>

After these changes CXF returns the content as expected.

 

Found the post useful? Subscribe to my newsletter for more free content, tips and tricks on IT & Java: