原因:org.bson.codecs.configuration.CodecConfigurationException:找不到类com.example.Hobbies的编解码器

时间:2019-04-10 04:15:18

标签: spring spring-data-mongodb

如何使用X86插入嵌入的文档?当我尝试插入嵌入式文档时,出现如下所示的错误。

例如,我想为Date和firstName保存空值。我使用Spring Data Mongo尝试了一些选项,但是效果不佳。

我遇到以下错误:

BSONObject

Person.java

java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:816) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:797) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:324) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at com.example.MongoPocApplication.main(MongoPocApplication.java:24) [classes/:na]
Caused by: org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class com.example.Hobbies.
    at org.bson.codecs.configuration.CodecCache.getOrThrow(CodecCache.java:46) ~[bson-3.8.2.jar:na]
    at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:63) ~[bson-3.8.2.jar:na]
    at org.bson.codecs.configuration.ChildCodecRegistry.get(ChildCodecRegistry.java:51) ~[bson-3.8.2.jar:na]
    at com.mongodb.DBObjectCodec.writeValue(DBObjectCodec.java:231) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.DBObjectCodec.encodeIterable(DBObjectCodec.java:292) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.DBObjectCodec.writeValue(DBObjectCodec.java:219) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.DBObjectCodec.encode(DBObjectCodec.java:149) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.DBObjectCodec.encode(DBObjectCodec.java:65) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.BasicDBObject.toJson(BasicDBObject.java:194) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.BasicDBObject.toJson(BasicDBObject.java:167) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.BasicDBObject.toJson(BasicDBObject.java:154) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.BasicDBObject.toString(BasicDBObject.java:238) ~[mongodb-driver-core-3.8.2.jar:na]
    at java.lang.String.valueOf(String.java:2994) ~[na:1.8.0_171]
    at java.io.PrintStream.println(PrintStream.java:821) ~[na:1.8.0_171]
    at com.example.MongoPocApplication.savePersons(MongoPocApplication.java:70) [classes/:na]
    at com.example.MongoPocApplication.run(MongoPocApplication.java:34) [classes/:na]
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:813) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]

爱好:

@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@Document
public class Person {
    @Id
    private String id;
    @Field
    private String firstName;
    @Field
    private String lastName;

    @Field
    private String emailId;
    @Field
    private List<Hobbies> hobbies;
}

主要

@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Hobbies {
    private String interest;
    private String sports;
}

2 个答案:

答案 0 :(得分:1)

我在一个班上发现了同样的问题。问题是您声明的不只是bean的构造函数。不知道为什么,以及如何与异常消息联系起来。

@AllArgsConstructor
@NoArgsConstructor

这里的问题不是Lombok,而是Lombok将两个构造函数插入到您的类中的事实:

public Person(String id, 
              String firstName, 
              String lastName, 
              String emailId,
              List<Hobbies> hobbies) { /* ... */ }

public Person() { /* ... */ }

删除两个构造函数之一,编解码器的问题将消失。

我希望这会有所帮助。

答案 1 :(得分:0)

我能够如下解决此问题。我想知道我们是否可以使用Spring Data Mongo Repository查询做到这一点?

Document hobbies1 = new Document();
hobbies1.put("interest", "Indoor");
hobbies1.put("sports", "Chess");

Document hobbies2 = new Document();
hobbies2.put("Loveoor", "Table Tennis");
hobbies2.put("Gamedoor", "Cricket");

BSONObject personBsonObj = BasicDBObjectBuilder.start()
    .add("firstName","John")
    .add("lastName", null)
    .add("email","john.kerr@gmail.com")
    .add("hobbies",Arrays.asList(hobbies1, hobbies2)).get();
BSONObject insert = mongoTemplate.insert(personBsonObj,"person");
    System.out.println(insert);