我正在了解弹簧靴,但是我因这个问题而停下来。我有一个@JsonSubTypes的人员抽象类,一个Client和Seller类,它扩展了@JsonTypeName的Person。所有请求均返回错误:尝试解析子类型时缺少类型ID。
人员班
<!-- language: python -->
def format_name(first_name, last_name):
if last_name != '' and first_name != '':
string = "Name: " + last_name + ", " + first_name
elif first_name != ' ' or last_name !=' ':
string = ("Name: " + first_name + last_name)
else:
string = ''
return string
客户端类
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = As.PROPERTY, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = Client.class, name = "client"),
@JsonSubTypes.Type(value = Seller.class, name = "seller")
// @JsonSubTypes.Type(value = Provider.class, name = "provider"),
})
public abstract class Person implements Comparable<Person>{
@Id
@Field("_id")
@JsonIgnore
private String code;
private String name;
private String phone;
private String email;
@Field("created_at")
private DateTime registerDate;
public Person(String name, String phone, String email) {
this.name = name;
this.phone = phone;
this.email = email;
}
卖方课程
@Document("clients")
@JsonTypeName("client")
public class Client extends Person{
@Indexed(unique=true)
private String cpf;
@Field("credit_limit")
private Double creditLimit;
public Client(String name, String phone, String email, String cpf,
Double creditLimit) {
super(name, phone, email);
this.cpf = cpf;
this.creditLimit = creditLimit;
}
初始方法
@Document("sellers")
@JsonTypeName("seller")
public class Seller extends Person {
@Indexed(unique=true)
private String cpf;
@Field("monthly_goal")
private Double monthlyGoal;
public Seller(String name, String phone, String email, String cpf,
Double monthlyGoal) {
super(name, phone, email);
this.cpf = cpf;
this.monthlyGoal = monthlyGoal;
}
答案 0 :(得分:0)
似乎您缺少在JSON请求正文中提交"type":"client"
字段。
但是,您还需要在Person
,Client
,Seller
类中提供默认构造函数,或使用@JsonCreator
批注标记可用的构造函数:
// Client with explicit creator
@JsonCreator
public Client(
@JsonProperty("name") String name,
@JsonProperty("phone") String phone,
@JsonProperty("email") String email,
@JsonProperty("cpf") String cpf,
@JsonProperty("creditLimit") Double creditLimit) {
super(name, phone, email);
this.cpf = cpf;
this.creditLimit = creditLimit;
}
// ------
// seller: use default constructor (also to be added in `Person`) for Jackson
public Seller() {}
// person
public Person() {}