gpt4 book ai didi

spring-data - Spring Boot & MongoDB 如何删除 '_class' 列?

转载 作者:行者123 更新时间:2023-12-03 14:10:55 25 4
gpt4 key购买 nike

当将数据插入 MongoDB Spring Data 时添加自定义“_class”列,在使用 Spring Boot & MongoDB 时有没有办法消除“class”列?

还是我需要创建自定义类型映射器?

任何提示或建议?

最佳答案

该问题的最新答案,使用嵌入式 mongo db 进行测试用例:
我引自 http://mwakram.blogspot.fr/2017/01/remove-class-from-mongo-documents.html

Spring Data MongoDB adds _class in the mongo documents to handle polymorphic behavior of java inheritance. If you want to remove _class just drop following Config class in your code.


package com.waseem.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.convert.DbRefResolver;
import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
import org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;

@Configuration
public class MongoConfig {

@Autowired MongoDbFactory mongoDbFactory;
@Autowired MongoMappingContext mongoMappingContext;

@Bean
public MappingMongoConverter mappingMongoConverter() {

DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDbFactory);
MappingMongoConverter converter = new MappingMongoConverter(dbRefResolver, mongoMappingContext);
converter.setTypeMapper(new DefaultMongoTypeMapper(null));

return converter;
}
}

关于spring-data - Spring Boot & MongoDB 如何删除 '_class' 列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23517977/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com