我有一个名为“items”的 mongo 系列,其中包含珠宝。 在集合的每个文档中,我都有一个鉴别器属性“类别” - 我使用杰克逊多态注释将文档反序列化为特殊的类,如 Pendatnt、Ring 等。 我正在使用抽象类和一些实现来获取项目。 我收到一条错误消息: “无法实例化……使用构造函数保护的珠宝……珠宝” Jewelery.java 是我的父抽象类, 这意味着框架尝试使用抽象类而不是使用我的实现。 看来杰克逊“@JsonSubTypes”注释被完全忽略了,我不明白为什么[所有这些设置过去都可以100%正常工作,但最近我发现我不再这样做了]
这是抽象类的示例,后面是 5 个实现之一。
/***************Jewelery.java****************************/
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.EXISTING_PROPERTY,
property = "category",
visible = true
)
@JsonSubTypes({
@JsonSubTypes.Type(value = Bracelet.class, name = "BRACELETS"),
@JsonSubTypes.Type(value = Earring.class, name = "EARRINGS"),
@JsonSubTypes.Type(value = Pendant.class, name = "PENDANTS"),
@JsonSubTypes.Type(value = Ring.class, name = "RINGS"),
@JsonSubTypes.Type(value = Set.class, name = "SETS")
})
@Document(collection = "items")
@Getter
@Setter
public abstract class Jewelery implements Sellable {
@Id
private String _id;
private String catId;`your text`
private String name;
private Category category; // this is the discriminator property!
private Integer importance;
private Integer price;
private Integer inStockQuantity;
private Boolean isDraft;
private Boolean isOnSale;
private Integer rating;
private Integer priority;
protected String[] images; // single item images
//single item properties;
private String[] materials;
private Size size;
private String _class;
protected Jewelery(String _id, String catId, String name, Category category, Integer importance,Integer price, Integer inStockQuantity, Boolean isDraft, Boolean isOnSale, Integer rating, Integer priority, String[] images, String[] materials, Size size, String _class) {
this._id = _id;
this.catId = catId;
this.name = name;
this.category = category;
this.importance = importance;
this.price = price;
this.inStockQuantity = inStockQuantity;
this.isDraft = isDraft;
this.isOnSale = isOnSale;
this.rating = rating;
this.priority = priority;
this.images = images;
this.materials = materials;
this.size = size;
this._class = _class;
}
}
/*************Bracelet.java****************/
@TypeAlias("Bracelet")
public class Bracelet extends Jewelery implements Sellable {
public Bracelet(String _id, String catId, String name, Category category, Integer importance, Integer price, Integer inStockQuantity, Boolean isDraft, Boolean isOnSale, Integer rating, Integer priority, String[] images, String[] materials, Size size, String _class) {
super(_id, catId, name, category, importance, price, inStockQuantity, isDraft, isOnSale, rating, priority, images, materials, size, _class);
}
@Override
public String[] getImages() {
return images;
}
@Override
public void setImages(String[] images) {
this.images = images;
}
}
/*************my repository class**********************************/
@Repository("items")
public interface JeweleryRepository extends MongoRepository<Jewelery, String> {
}
/*****my service call***********************************/
public List<Jewelery> getItems(String sortBy) {
return jewelryRepo.findAll();
}
我尝试从父抽象类中删除
@Document(collection = "items")
并将此注释放在所有实现上。它不起作用,因为 mongo 无法识别集合名称,并且默认情况下会尝试从不存在的“珠宝”集合中获取项目,从而导致空数组响应。
您可以使用转换器和映射配置来解决。
定义自己的Writing Converter和Reading Converter,将它们注册到AbstractMongoClientConfiguration中