micronaut reactive-mongo 设置内部服务器错误

micronaut reactive-mongo setup Internal Server Error

我正在尝试在基于 java 的 micronaut 项目中设置 mongo 反应式客户端,但出现以下错误:

"Internal Server Error: An exception occurred when decoding using the AutomaticPojoCodec.\nDecoding into a 'Member' failed with the following exception:\n\nCannot find a public constructor for 'Member'.\n\nA custom Codec or PojoCodec may need to be explicitly configured and registered to handle this type."

我的项目设置如下:

├── Application.java
├── config
│   └── MongoConfiguration.java
├── controller
│   └── MemberController.java
├── model
│   └── Member.java
└── service
    └── MemberService.java

应用程序正在启动,但如果我调用 http 端点列出所有成员,它会抛出一个错误,如 post.

中所列

我的会员一开始看起来很简单:

import com.fasterxml.jackson.annotation.JsonProperty;

public class Member {
    private final String firstname;
    private final String lastname;

    public Member( @JsonProperty("firstname") String firstname, @JsonProperty("lastname") String lastname) {
        this.firstname = firstname;
        this.lastname = lastname;
    }

   ....

}

// And my service, has this method where I'm calling mongo
    private MongoCollection<Member> getCollection() {
        configuration.setCollectionName("members");
        return mongoClient.getDatabase(configuration.getDatabaseName())
                .getCollection(configuration.getCollectionName(), Member.class);
    }

我知道设置有什么问题或需要更多信息吗?

感谢帮助

你的收缩器应该用 @JsonCreator 注释,因为 Member 没有默认构造函数并且有带有参数注释的自定义构造函数 @JsonProperty

Constructor/factory method where every argument is annotated with either JsonProperty or JacksonInject, to indicate name of property to bind to

public class Member {
    private final String firstname;
    private final String lastname;

    @JsonCreator
    public Member(@JsonProperty("firstname") String firstname, 
                  @JsonProperty("lastname") String lastname) {
        this.firstname = firstname;
        this.lastname = lastname;
    }

   ....

}