【发布时间】:2018-08-07 19:53:15
【问题描述】:
我正在尝试将 不可变 对象从 MongoDB 映射到我的 Java POJO,但我不断收到以下错误:
org.springframework.web.util.NestedServletException:
Request processing failed;
nested exception is java.lang.RuntimeException:
org.mongodb.morphia.mapping.MappingException:
No usable constructor for com.example.model.Item
似乎在使用不可变对象时,我需要使用@BsonCreator 进行注释,但这似乎不起作用,我相信这可能是因为使用此注释需要我以某种方式配置org.bson.codecs.pojo.Conventions#ANNOTATION_CONVENTION。也许我是盲人,但我似乎无法在任何地方找到任何关于如何配置它的示例。任何帮助将不胜感激。这是我带注释的 POJO:
@Value /* Lombok auto generates getters */
@Builder /* Lombok auto generates builder method */
public class Item implements Serializable {
private final @NotNull AnEnum type;
private final int refId;
private final int quantity;
@BsonCreator
public Item(@BsonProperty("type") AnEnum type,
@BsonProperty("refId") int refId,
@BsonProperty("quantity") int quantity) {
this.type = type;
this.refId = refId;
this.quantity = quantity;
}
}
【问题讨论】:
-
您使用的是 Morphia 还是纯 Java 驱动程序? @BsonCreator 是驱动程序的一部分,但您添加了 [morphia] 关键字。
标签: java mongodb spring-boot morphia mongo-java-driver