I'm learn about spring boot, however i stopped with this problem. I Have an person abstract class with @JsonSubTypes,a Client and Seller class what extends Person with @JsonTypeName. All requests return the error: Missing type id when trying to resolve subtype.
Person class
@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;
}
Client class
@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;
}
Seller class
@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;
}
Initial method
@PostMapping("sale/store")
@ResponseBody
public ResponseEntity<String> store(@RequestBody Client client) {
return ResponseEntity.ok("ok");
}
It seems that you're missing to submit "type":"client"
field in the JSON request body. But you'll also need to provide default constructors in Person
, Client
, Seller
classes or mark available constructors with @JsonCreator
annotation:
// 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() {}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.