出现错误“找不到类 org.springframework.data.mongodb.core.convert.DefaultDbRefResolver$LazyLoadingInterceptor"的序列化程序 [英] Getting error "No serializer found for class org.springframework.data.mongodb.core.convert.DefaultDbRefResolver$LazyLoadingInterceptor"

查看:30
本文介绍了出现错误“找不到类 org.springframework.data.mongodb.core.convert.DefaultDbRefResolver$LazyLoadingInterceptor"的序列化程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 spring 数据 mongodb,在执行延迟加载后,我收到错误 找不到类 org.springframework.data.mongodb.core.convert.DefaultDbRefResolver$LazyLoadingInterceptor 的序列化程序".

I am using spring data mongodb,after doing lazy loading true i am getting error "No serializer found for class org.springframework.data.mongodb.core.convert.DefaultDbRefResolver$LazyLoadingInterceptor".

我的域类是

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;

}

我已经浪费了很多时间,但我无法得到合适的答案?谁能帮我重新爱上它?提前致谢

i have wasted my time alot,but i am unable to get suitable answer of it? anyone can help me to reslove it? Thanks in advance

推荐答案

根据您的要求添加此配置代码

Add this configuration code for your requirement

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

}

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

希望可以帮到你!

这篇关于出现错误“找不到类 org.springframework.data.mongodb.core.convert.DefaultDbRefResolver$LazyLoadingInterceptor"的序列化程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆