Christian Kalkhoff
Christian Kalkhoff

Reputation: 192

How to use Jackson as JSON provider for JAX-RS-Client instead of Johnzon in TomEE 7?

I use TomEE 7.0.1 with Jackson 2 as JAX-RS JSON provider (configured in openejb-jar.xml). Posting json to jax-rs services works perfectly well and uses the jackson annotations.

But using JAX-RS client (or cxf webclient) always uses Johnzon even if I provide Jackson to the Webclient.create method. After some debugging I am sure that TomEE adds Johnzon to the client factory somehow and no other message body parser for json can be used.

Is this a bug with the TomEE CXF integration or do I miss something?

Upvotes: 6

Views: 3530

Answers (2)

Another solution is to disable the Johnzon TomEEJson*Provider. You can do it either in the conf/system.properties by adding the following properties :

org.apache.openejb.server.cxf.rs.johnzon.TomEEJsonpProvider.activated = false
org.apache.openejb.server.cxf.rs.johnzon.TomEEJsonbProvider.activated = false

or in your META-INF/openejb-jar.xml :

<?xml version="1.0" encoding="UTF-8"?>
<openejb-jar xmlns="http://www.openejb.org/openejb-jar/1.1">
    <pojo-deployment  class-name="jaxrs-application">
        <properties>
            cxf.jaxrs.providers = com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider
            org.apache.openejb.server.cxf.rs.johnzon.TomEEJsonbProvider.activated = false
            org.apache.openejb.server.cxf.rs.johnzon.TomEEJsonpProvider.activated = false
        </properties>
    </pojo-deployment>
</openejb-jar>

Hope it helps.

Upvotes: 0

Romain Manni-Bucau
Romain Manni-Bucau

Reputation: 3422

Johnzon is registered on the bus by default to have a default JSON (mapping) and JSON-P provider. CXF is clever enough to make bus providers with a lower priority than applications ones (register() for client API) so if you call register you expect to use jackson...and still use johnzon - I suspect it is the case you hit.

That's perfectly normal and due to two things:

  1. JAX-RS specification priority definition
  2. Jackson Consumes/Produce definition

To summarize (1) says that more specific is the provider higher is its priority so an "application/json" provider will be priveledged in favor of a "/" one.

To avoid issues Johnzon uses "application/json". However jackson uses a custom matching strategy and therefore uses "/". So the bus priority is ignored since mediatype priority is enough to say johnzon is "more adapted" than jackson for json.

To solve it the easiest is likely to override jackson provider (just extend it) and decorate it with @Provides/@Consumes with MediaType.APPLICATION_JSON instead of wildcard one.

Upvotes: 13

Related Questions