Cannot get TomEE to use Jackson

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Cannot get TomEE to use Jackson

Thiesius
Hello,

no matter how hard I try I just cannot get TomEE to use Jackson instead of Johnzon. Want to use Jersey with Jackson

I tried to:
 - Set the providers "cxf.jaxrs.providers" in openejb-jar.xml
  ( log entry: org.apache.openejb.server.rest.RESTService.deployApplication Registered JAX-RS Configuration: ...)
 - Extend Jackson provider as

@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class OurJsonProvider
        extends com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider {}

I have pom dependencies:
    <dependency>
            <groupId>com.fasterxml.jackson.jaxrs</groupId>
            <artifactId>jackson-jaxrs-json-provider</artifactId>
            <version>2.9.0</version>
     </dependency>

     <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
            <version>2.26-b09</version>
      </dependency>

etc.

Yet whenever I try to serialize object I get

Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: ..., could not initialize proxy - no Session
        at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:582)
        at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:201)
        at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:561)
        at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:132)
        at org.hibernate.collection.internal.PersistentBag.iterator(PersistentBag.java:277)
        at org.apache.johnzon.mapper.MappingGeneratorImpl.writeValue(MappingGeneratorImpl.java:315)

and a lot more johnzon stacktracke lines...

Obviously because I have my properties annotated with Jackson and the object is serialized with Johnzon instead and it's ignoring my annotations.

What am I doing wrong?

Thank you
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Cannot get TomEE to use Jackson

cocorossello
This post was updated on .
The error is a problem with hibernate and lazy collections. Try returning just a mocked object, not from database.

Jackson works  in tomee, to get it working you need a @Provider

@Provider
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class JacksonJSONProvider extends com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider {

    public JacksonJSONProvider() {
        super();
        setMapper(JsonParser.defaultJacksonObjectMapper()); //Your mapper
    }

}

(In your case you can leave the constructor empty)

See http://stackoverflow.com/questions/38904640/how-to-use-jackson-as-json-provider-for-jax-rs-client-instead-of-johnzon-in-tome
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Cannot get TomEE to use Jackson

Thiesius
I wasn't pointing out the problem with lazy collections. I had @JsonIgnore on that property. But since the Jackson didn't kick in, Johnzon skipped the annotation and tried to serialize the property.

Anyways the problem was indeed with the @Provider annotation. Thank you very much for helping me fixing this.

Anyways why is it such a hassle to get it working under TomEE?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Cannot get TomEE to use Jackson

Romain Manni-Bucau
2017-08-10 19:06 GMT+02:00 Thiesius <[hidden email]>:

> I wasn't pointing out the problem with lazy collections. I had @JsonIgnore
> on
> that property. But since the Jackson didn't kick in, Johnzon skipped the
> annotation and tried to serialize the property.
>
> Anyways the *problem was indeed with the @Provider* annotation. Thank you
> very much for helping me fixing this.
>
> Anyways why is it such a hassle to get it working under TomEE?
>
>
Well it is not linked to tomee by itself. Jackson defines himself as being
a fallback provider so used if no other provider matches the request (based
on JAX-RS 2 rules). Since johnzon is defined to match json as the last one
(so if you drop another json provider it is taken automatically) it has a
higher priority then jackson. If jackson defines himself as matching json
then it would be transparent.



>
>
> --
> View this message in context: http://tomee-openejb.979440.
> n4.nabble.com/Cannot-get-TomEE-to-use-Jackson-tp4682409p4682416.html
> Sent from the TomEE Users mailing list archive at Nabble.com.
>
Loading...