Estou tentando enviar um JSON ao meu método Spring para inserção no banco de dados mas recebo a seguinte mensagem:
JSON parse error:
Cannot construct instance of br.com.marketHubServer.model.Collaborator (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of br.com.marketHubServer.model.Collaborator (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator)\n at [Source: (PushbackInputStream); line: 6, column: 9]
Acredito que o problema aconteça devido a herança de classes que estou fazendo, pois com outras classes o erro não acontece.
Classe Perfil:
@Entity(name="Profile")
@Inheritance(strategy = InheritanceType.JOINED)
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME,
include=JsonTypeInfo.As.EXISTING_PROPERTY, property="type")
@JsonTypeName("profile")
@JsonSubTypes({
@JsonSubTypes.Type(name="company", value=Company.class),
@JsonSubTypes.Type(name="collaborator", value=Collaborator.class)})
public abstract class Profile {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Transient
@JsonProperty("type")
private final String type = "profile";
@Column(unique = true, nullable = false, length = 255)
private String emailAddress;
@JsonIgnore
private String password;
@Transient
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private String newPassword;
@Column(nullable = false, length = 100)
private String name;
@ElementCollection(fetch = FetchType.EAGER)
private List<String> permissions;
Classe empresa:
@Entity
public class Company extends Profile {
@Transient
@JsonProperty("type")
private final String type = "company";
@Column(nullable = false, length = 100)
private String companyLegalName;
@Column(nullable = false, length = 400)
private String companyDescription;
@Column(nullable = false, length = 14)
private String companyCnpjCpf;
@Column(nullable = false, length = 9)
private String companyPhone;
Classe Colaborador:
@Entity
public class Collaborator extends Profile {
@Transient
@JsonProperty("type")
private final String type = "collaborator";
@Column(nullable = false, length = 11)
private String collaboratorCpf;
Método para consistir perfil no banco de dados:
@RequestMapping(path = "/companies", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public Profile create(@AuthenticationPrincipal ProfileAut profileAut, @RequestBody Profile profile) throws Exception {
profile.setId(0);
Profile profileSave = companyDAO.save(profile);
return profileSave;
}
Estou tentando salvar um colaborador no banco de dados através do seguinte json:
{
"emailAddress":"teste",
"password":"teste2",
"name":"teste3",
"type":"collaborator",
"collaboratorCpf": "teste"
}
Sem utilização de herança consegui receber o objeto e armazenar os dados no banco de dados sem problemas.