RESTEasy with jackson 导致序列化 JsonMappingException

RESTEasy with jackson causes serializing JsonMappingException

当我尝试使用 RESTEasy 检索数据时,出现以下异常:

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"])

我在网上查了下,发现是因为Jackson试图序列化数据,但还没有加载。我在某个地方发现了通过使用这样的杰克逊配置来禁用异常的可能性:

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);
    }
}

但我不知道如何让它工作,我的意思是,方法 setMapper 应该是什么样子?我没有使用 Spring。我还尝试用以下

注释 'Email' class
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})

没用。我不想注释每个 getter 至极 '@JsonProperty' 左右,我真的很想使用配置 Class.

禁用此异常

这是我的依赖项:

<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>

由于 RESTEasy 是 JAX-RS 实现,您可以使用 JAX-RS 方式自定义 Jackson 的 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;
    }
}