引起:org.bson.codecs.configuration.CodecConfigurationException:找不到类 com.example.Hobbies 的编解码器 [英] Caused by: org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class com.example.Hobbies

查看:21
本文介绍了引起:org.bson.codecs.configuration.CodecConfigurationException:找不到类 com.example.Hobbies 的编解码器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

我收到以下错误:

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

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

爱好:

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

主要

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

推荐答案

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

I found the same problem in one of my classes. The problem is that you declared more than a constructor for your bean. Don't know why, and how to relate with the message of the exception.

@AllArgsConstructor
@NoArgsConstructor

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

The problem here is not Lombok, but the fact that Lombok inserts into your class two constructors:

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

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

删除两个构造函数中的一个,编解码器的问题就会消失.

Delete one of the two constructors, and the problem with the codec will go away.

希望能帮到你.

这篇关于引起:org.bson.codecs.configuration.CodecConfigurationException:找不到类 com.example.Hobbies 的编解码器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆