Spring REST存储库继承null值



我在Spring Rest存储库中映射父子类时遇到了一些问题。我的情况是,我需要使用Spring Security对这两种类型的用户进行身份验证,但我也希望能够为所有者分配特定类型的属性,如Pets和Employee的salary。这是实现这一目标的良好结构吗?如果是,请帮助解决这个问题。

我已经在一个表中插入了用dtype列区分的值,但无法插入除dtype之外的任何其他值。

我使用最新的SpringBoot(默认的JSON映射器(和MSSQL。

这是我POST请求的有效载荷:

{
"username": "admin",  // null in db
"firstName": "Delacruz", // null in db
"lastName": "House", // null in db
"dtype": "Owner" // present in db
}

这是我的班级结构:
存储库

@RepositoryRestResource(path = "user", collectionResourceRel = "user")
public interface UserRepository extends PagingAndSortingRepository<User, Long> {
}

父类-用户

@Entity
@Table(name = "users")
@Inheritance
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME,
include=JsonTypeInfo.As.EXISTING_PROPERTY,
property="dtype")
@JsonSubTypes({
@JsonSubTypes.Type(name="Owner", value=Owner.class),
@JsonSubTypes.Type(name="Employee", value=Employee.class)})
@RestResource(path="user")
public abstract class User{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column
private String username;
// rest of properties
}

员工-第一个儿童类

@EqualsAndHashCode(callSuper = true)
@Entity
@Data
public class Employee extends User {
private String type = "EMPLOYEE";
@Column
@Enumerated
private EmployeeType employeeType;
@Column
private LocalDate dateOfEmployment;
//rest of properties
}

所有者-另一个子类

@EqualsAndHashCode(callSuper = true)
@Data
@Entity
public class Owner extends User{
@OneToMany(mappedBy = "owner")
private List<Pet> pets;
}

向子类添加以下构造函数(相应于所有者(解决了问题:

@JsonCreator
public Employee(
@JsonProperty(value = "username") String username,
@JsonProperty(value = "firstName") String firstName,
@JsonProperty(value = "lastName") String lastName,
@JsonProperty(value = "password") String password,
@JsonProperty(value = "email") String email,
@JsonProperty(value = "phoneNumber") String phoneNumber
) {
super(username, firstName, lastName, password, email, phoneNumber);
}

最新更新