I'm trying to setup the mongo reactive client in a micronaut project based in java and I'm getting the following error:
"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."
my project setup is looing as follows:
├── Application.java
├── config
│ └── MongoConfiguration.java
├── controller
│ └── MemberController.java
├── model
│ └── Member.java
└── service
└── MemberService.java
The app is launching but if I am calling the http endpoint to list all members it throws an Error as listed here in the post.
My Member is looking quite simple for the beginning:
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);
}
Any I idea whats wrong in the setup or do need more information?
thx for the help
Your constrictor should be annotated with @JsonCreator, because Member
doesn't have default construcor and have custom constructor with arguments annotated @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;
}
....
}