【问题标题】:RESTEasy with jackson causes serializing JsonMappingExceptionRESTEasy with jackson 导致序列化 JsonMappingException
【发布时间】:2016-09-25 02:16:22
【问题描述】:

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

原因:org.codehaus.jackson.map.JsonMappingException:否 为类找到序列化程序 org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer 并且没有 发现创建 BeanSerializer 的属性(为了避免异常, 禁用 SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) )(通过 参考链: 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"])

我在互联网上查看并发现,那是因为杰克逊试图序列化尚未加载的数据(尚未加载)。我在某处发现了通过使用这样的杰克逊配置来禁用异常的可能性:

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 应该如何?我没有使用弹簧。我还尝试使用以下注释“电子邮件”类

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

它没有帮助。我不想用'@JsonProperty'左右注释每个getter,我真的很想使用配置类禁用这个异常。

这是我的依赖项:

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

【问题讨论】:

    标签: json hibernate jackson resteasy


    【解决方案1】:

    由于 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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-26
      • 2016-07-09
      • 1970-01-01
      • 1970-01-01
      • 2012-12-07
      • 1970-01-01
      相关资源
      最近更新 更多