从MongoDb属性中删除_class属性Spring Java(不同的困境)

时间:2016-09-15 11:06:56

标签: java spring mongodb spring-boot

首先,我使用Google搜索/研究其他解决方案,但它们不能解决我的问题。

public class BasicRule {

  public BasicRule() {
  }

  @Id
  private String id;

  @Field("fname")
  @JsonInclude(JsonInclude.Include.ALWAYS)
  private int fname;

  @Field("sname")
  @JsonInclude(JsonInclude.Include.ALWAYS)
  private int sname;

  @Field("gender")
  @SerializedName("gender")
  @JsonInclude(JsonInclude.Include.ALWAYS)
  private int gender;

  //getter setter

  public BasicRule(String id, int fname, int sname, int gender) {
    this.id = id;
    this.fname = fname;
    this.sname = sname;
    this.gender = gender;
}

这是我想要插入数据库的数据。但它正确地执行了此操作

  "_class" : "BasicRule"

总是包含在内。

我使用MongoRepository,它有一个直接保存MongoDb的保存方法。

public void saveBasicRules(List<BasicRule> basicRuleList) {
    basicRuleRepository.save(basicRuleList);
}

我尝试了解决方案

public @Bean
MongoDbFactory mongoDbFactory() throws Exception {
   return new SimpleMongoDbFactory(new Mongo(), "database");
}

public @Bean
MongoTemplate mongoTemplate() throws Exception {
   //remove _class
   MappingMongoConverter converter = new MappingMongoConverter(
       mongoDbFactory(), new MongoMappingContext());
   converter.setTypeMapper(new DefaultMongoTypeMapper(null));

   MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory(), converter);

   return mongoTemplate;
}

我尝试在BasicRule类中添加上面的内容,但它没有帮助。有什么建议吗?

0 个答案:

没有答案