Caused by: org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class com.example.Hobbies

天涯浪子 提交于 2019-12-08 10:03:00

问题


How to insert the embedded documents using BSONObject? When I am trying to insert the embedded document, getting error shown below.

I wanted to save null values for the Date and firstName for example. I tried some options using Spring Data Mongo, but it doesn't work out well.

I am getting below error:

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]

Person.java

@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;
}

Hobbies:

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

Main

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

        BSONObject insert = mongoTemplate.insert(personBsonObj,"person");
        System.out.println(insert);

回答1:


I was able to solve this issue like below. I wonder if we can do this using Spring Data Mongo Repository query ?

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);


来源:https://stackoverflow.com/questions/55604660/caused-by-org-bson-codecs-configuration-codecconfigurationexception-cant-find

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!