MongoTemplate 移除 _class 字段


@Configuration
public class ApplicationReadyListener implements ApplicationListener<ContextRefreshedEvent> {

  @Autowired
  MongoTemplate mongoTemplate;

  @Override
  public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
    MongoConverter converter = mongoTemplate.getConverter();
    if (converter.getTypeMapper().isTypeKey("_class")) {
      ((MappingMongoConverter) converter).setTypeMapper(new DefaultMongoTypeMapper(null));
    }
  }
}

参考:

Spring Boot 学习笔记:MongoTemplate 移除 _class 字段

posted @ 2019-08-09 10:33  VictorBu  阅读(2168)  评论(0编辑  收藏  举报