1
votes

When I try to retrieve data using RESTEasy I get the following exception:

Caused by: org.codehaus.jackson.map.JsonMappingException: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: les.core.modules.profile.Profile["emails"]->org.hibernate.collection.internal.PersistentSet[0]->mapping.social.employee.email.Email["state"]->mapping.system.enums.DataState_$$_javassist_172["handler"])

I looked on the internet and found out, that is because the Jackson tries to serialize data, which is not loaded (yet). I found somewhere a possibility to disable the exception by using such a jackson config:

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.codehaus.jackson.jaxrs.JacksonJsonProvider;

public class JacksonConfig extends JacksonJsonProvider {
    public JacksonConfig() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
//        setMapper(mapper);
    }
}

But I don't know how to make it work, I mean, how should the method setMapper look like? I am not using Spring. I also tried to annotate the 'Email' class with following

@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})

It didn't help. I wouldn't like to annotate each getter wich '@JsonProperty' or so, I would really like to disable this exception using a config Class.

here are my dependencies:

<dependencies>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jaxrs</artifactId>
            <version>3.0.4.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jackson-provider</artifactId>
            <version>3.0.4.Final</version>
        </dependency>
</dependencies>
1

1 Answers

4
votes

Since RESTEasy is a JAX-RS implementation, you can use the JAX-RS way of customizing Jackson's ObjectMapper:

import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

/**
 * ContextResolver that automatically configures and provides {@link ObjectMapper} used by Jackson.
 */
@Provider
public class ObjectMapperContextResolver implements ContextResolver<ObjectMapper> {

    private final ObjectMapper objectMapper = new ObjectMapper()
            .disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);

    @Override
    public ObjectMapper getContext(Class<?> type) {
        return objectMapper;
    }
}