自学内容网 自学内容网

springboot mongodb 数据添加时更改‘_class‘字段

重写beanMappingMongoConverter

修改DefaultMongoTypeMapper

 /**
     * 更改默认的 MongoDB 映射转换器,更改'_class'字段
     *
     * @param factory     MongoDB 工厂对象
     * @param context     MongoDB 映射上下文
     * @param beanFactory springBean 工厂
     * @return MongoDB 映射转换器
     */
    @Bean
    public MappingMongoConverter mappingMongoConverter(MongoDbFactory factory, MongoMappingContext context, BeanFactory beanFactory) {
        DbRefResolver defaultDbRefResolver = new DefaultDbRefResolver(factory);
        MappingMongoConverter mappingMongoConverter = new MappingMongoConverter(defaultDbRefResolver, context);
        try {
            mappingMongoConverter.setCustomConversions(beanFactory.getBean(CustomConversions.class));
        } catch (NoSuchBeanDefinitionException ignore) {
        }
        ///修改DefaultMongoTypeMapper的传参值,是什么_class就被改为什么,时值为null时,则没有_class字段
        mappingMongoConverter.setTypeMapper(new DefaultMongoTypeMapper(null));
        return mappingMongoConverter;
    }

原文地址:https://blog.csdn.net/weixin_44728369/article/details/137865143

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!