mongoTemplate 保存数据时去掉_class
新建一个MongoConfig类放在Config包下
package com
.xxx
.xxx
.config
;
import org
.springframework
.beans
.factory
.annotation
.Autowired
;
import org
.springframework
.context
.ApplicationListener
;
import org
.springframework
.context
.annotation
.Configuration
;
import org
.springframework
.context
.event
.ContextRefreshedEvent
;
import org
.springframework
.data
.mongodb
.core
.MongoTemplate
;
import org
.springframework
.data
.mongodb
.core
.convert
.DefaultMongoTypeMapper
;
import org
.springframework
.data
.mongodb
.core
.convert
.MappingMongoConverter
;
import org
.springframework
.data
.mongodb
.core
.convert
.MongoConverter
;
@Configuration
public class MongoConfig 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
));
}
}
}
测试成功,数据没有_class字段