mongodb - 出现错误 "No serializer found for class org.springframework.data.mongodb.core.convert.DefaultDbRefResolver$LazyLoadingInterceptor"

标签 mongodb spring-data-mongodb

我正在使用 spring data mongodb,在执行延迟加载 true 后,我收到错误“找不到类 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;

}

我已经浪费了很多时间,但我无法得到合适的答案?任何人都可以帮助我重新喜欢它吗? 提前致谢

最佳答案

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

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

希望这对您有帮助!

关于mongodb - 出现错误 "No serializer found for class org.springframework.data.mongodb.core.convert.DefaultDbRefResolver$LazyLoadingInterceptor",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31914090/

相关文章:

java - 复制按字段值分组

javascript - meteor :在单独的列表标签中显示 mongo 数组中的每个项目

mysql - 数据对象存储 - 表 JOIN 可以连接到单表 SELECT 不能吗?

mongodb - 为什么我的查询没有更新数组中的对象?

java - 如何使用存储库接口(interface)在 Spring Data 中通过其嵌套对象的 objectId 查找集合?

java - 如何将自定义转换器与@DataMongoTest 一起使用?

javascript - 如何使用 Node.js 服务器将图像数据从前端保存到 Mongoose 数据库

java - MongoDB 数据模型支持每个事件、每个日期范围的唯一访问者

mongodb - 从保存操作中排除空属性

java - Spring Data Mongo : How to save only date i. e。不是时间