首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >“未为类org.springframework.data.mongodb.core.convert.DefaultDbRefResolver$LazyLoadingInterceptor”找到序列化程序“

“未为类org.springframework.data.mongodb.core.convert.DefaultDbRefResolver$LazyLoadingInterceptor”找到序列化程序“
EN

Stack Overflow用户
提问于 2015-08-10 07:41:39
回答 2查看 1.1K关注 0票数 2

我正在使用spring,在完成延迟加载后,我得到了错误的"No serializer found for class org.springframework.data.mongodb.core.convert.DefaultDbRefResolver$LazyLoadingInterceptor"

我的域类是

代码语言:javascript
复制
public class Preference extends BaseEntity {

    @DBRef(lazy = true)
    User user;

    MetadataEnum preferenceType;


    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }


    public MetadataEnum getPreferenceType() {
        return preferenceType;
    }


    public void setPreferenceType(MetadataEnum preferenceType) {
        this.preferenceType = preferenceType;
    }


    public List<Subtype> getSubtypes() {
        return subtypes;
    }


    public void setSubtypes(List<Subtype> subtypes) {
        this.subtypes = subtypes;
    }

    List<Subtype> subtypes = new ArrayList<Subtype>();


    boolean enableSearch;

}

我浪费了很多时间,但我无法得到合适的答案?有人能帮我重新爱上它吗?提前感谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-08-10 12:55:42

为您的需求添加此配置代码

代码语言:javascript
复制
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.convert.LazyLoadingProxy;

import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;

@Configuration
public class LazyLoadingSerializationConfig {

    @Bean
    public ObjectMapper objectMapper() {

        ObjectMapper om = new ObjectMapper();
        final SimpleModule module = new SimpleModule("<your entity>", new Version(1, 0, 0,null));

        module.addSerializer(LazyLoadingProxy.class, new LazyLoadingSerializer());
        om.registerModule(module);

        return om;
    }

}

代码语言:javascript
复制
import java.io.IOException;

import org.springframework.data.mongodb.core.convert.LazyLoadingProxy;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

public class LazyLoadingSerializer extends JsonSerializer<LazyLoadingProxy> {

    @Override
    public void serialize(LazyLoadingProxy value, JsonGenerator jgen,
        SerializerProvider provider) throws IOException,
        JsonProcessingException {

        jgen.writeStartObject();
        jgen.writeStringField("id", value.toDBRef().getId().toString());
        jgen.writeEndObject();
    }
}

希望这能帮到你!

票数 2
EN

Stack Overflow用户

发布于 2015-08-10 12:46:16

关闭该bean的序列化程序,因为延迟加载@DBRef只对实体列表有效,jackson会自动序列化@DBRef(懒惰=真)列表用户,而不序列化‘@DBRef(懒惰= true)用户。因此,您必须将其序列化,或者关闭该bean的序列化程序。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31914090

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档