gpt4 book ai didi

java - micronautreactive-mongo 设置内部服务器错误

转载 作者:行者123 更新时间:2023-12-02 09:44:09 27 4
gpt4 key购买 nike

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

"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 端点来列出所有成员,它会抛出帖子中列出的错误。

我的成员(member)一开始看起来很简单:

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

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

感谢您的帮助

最佳答案

你的大 python 应该用 @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;
}

....

}

关于java - micronautreactive-mongo 设置内部服务器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56798375/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com