spring boot 中 mongodb 对象中移除 _class

参考了一下网上的帖子 https://stackoverflow.com/questions/23517977/spring-boot-mongodb-how-to-remove-the-class-column

简单的方式是:

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;

@Configuration
public class MongoDBConfig implements InitializingBean {

    // 参考:https://stackoverflow.com/questions/23517977/spring-boot-mongodb-how-to-remove-the-class-column
    @Autowired
    @Lazy
    private MappingMongoConverter mappingMongoConverter;

    @Override
    public void afterPropertiesSet() {
        mappingMongoConverter.setTypeMapper(new DefaultMongoTypeMapper(null));
    }
}

猜你喜欢

转载自www.cnblogs.com/hnustcomputer/p/12381468.html