发布新实体时如何在 Spring Data REST 中引用具有继承的实体?

tew*_*ewe 5 rest jackson spring-data-rest

我有加入继承的实体:

支持者

@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "supporterType")
@JsonSubTypes({
    @JsonSubTypes.Type(value = PersonSupporterEntity.class, name = "PERSON"),
    @JsonSubTypes.Type(value = CompanySupporterEntity.class, name = "COMPANY")
})
@DiscriminatorColumn(name="supporter_type")
@Table(name = "supporter")
public class SupporterEntity extends UpdatableEntity {
    private long id;
    private SupporterType supporterType;
    private PartnerEntity partner;
...
}
Run Code Online (Sandbox Code Playgroud)

个人支持者

@Entity
@DiscriminatorValue("PERSON")
@Table(name = "person_supporter")
public class PersonSupporterEntity extends SupporterEntity {
...
}
Run Code Online (Sandbox Code Playgroud)

公司支持者

@Entity
@DiscriminatorValue("COMPANY")
@Table(name = "company_supporter")
public class CompanySupporterEntity extends SupporterEntity {
...
}
Run Code Online (Sandbox Code Playgroud)

我有另一个引用 SupporterEntity 的实体

@Entity
@Table(name = "contact")
public class ContactEntity extends UpdatableEntity {

    private long id;
    private SupporterEntity supporter;
...
    @ManyToOne // same error with @OneToOne
    @JoinColumn(name = "supporter_id", referencedColumnName = "id", nullable = false)
    public SupporterEntity getSupporter() {
        return supporter;
    }
...
}
Run Code Online (Sandbox Code Playgroud)

存储库

@Transactional
@RepositoryRestResource(collectionResourceRel = "supporters", path = "supporters")
public interface SupporterEntityRepository extends JpaRepository<SupporterEntity, Long> {

    @Transactional(readOnly = true)
    @RestResource(path = "by-partner", rel = "by-partner")
    public Page<SupporterEntity> findByPartnerName(@Param("name") String name, Pageable pageable);
}
Run Code Online (Sandbox Code Playgroud)
@Transactional
@RepositoryRestResource(collectionResourceRel = "person_supporters", path = "person_supporters")
public interface PersonSupporterEntityRepository extends JpaRepository<PersonSupporterEntity, Long> {

}
Run Code Online (Sandbox Code Playgroud)
@Transactional
@RepositoryRestResource(collectionResourceRel = "company_supporters", path = "company_supporters")
public interface CompanySupporterEntityRepository extends JpaRepository<CompanySupporterEntity, Long> {

}
Run Code Online (Sandbox Code Playgroud)
@Transactional
@RepositoryRestResource(collectionResourceRel = "contacts", path = "contacts")
public interface ContactEntityRepository extends JpaRepository<ContactEntity, Long> {

    @Transactional(readOnly = true)
    @RestResource(path = "by-supporter", rel = "by-supporter")
    public ContactEntity findBySupporterId(@Param("id") Long id);
}
Run Code Online (Sandbox Code Playgroud)

我使用 Spring Boot、Spring Data REST、Spring Data JPA、Hibernate、Jackson。当我尝试使用这样的帖子请求创建新的 ContactEntity 时:

{
   "supporter":"/supporters/52",
   "postcode":"1111",
   "city":"Test City 1",
   "address":"Test Address 1",
   "email":"test1@email.com",
   "newsletter":true
}
Run Code Online (Sandbox Code Playgroud)

我得到这个例外:

Caused by: com.fasterxml.jackson.databind.JsonMappingException: Unexpected token (VALUE_STRING), expected FIELD_NAME: missing property 'supporterType' that is to contain type id  (for class com.facer.domain.supporter.SupporterEntity)
 at [Source: HttpInputOverHTTP@4321c221; line: 1, column: 2] (through reference chain: com.facer.domain.supporter.ContactEntity["supporter"])
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148) ~[jackson-databind-2.4.4.jar:2.4.4]
Run Code Online (Sandbox Code Playgroud)

经过 2 天的调试,我找到了一种方法,但我有点猜对了。所以如果我像这样发布它:

{
   "supporter":{
      "supporterType":"PERSON",
      "id":"52"
   },
   "postcode":"1111",
   "city":"Test City 1",
   "address":"Test Address 1",
   "email":"test1@email.com",
   "newsletter":true
}
Run Code Online (Sandbox Code Playgroud)

它有效,但我不知道为什么。另一个请求有什么问题?当引用的实体没有继承时,它在其他任何地方都一样。

a b*_*ver 1

这看起来像是杰克逊的问题。具体来说,就是下面的代码com.fasterxml.jackson.databind.deser.SettableBeanProperty

if (_valueTypeDeserializer != null) {
   return _valueDeserializer.deserializeWithType(jp, ctxt, _valueTypeDeserializer);
}
return _valueDeserializer.deserialize(jp, ctxt);
Run Code Online (Sandbox Code Playgroud)

如果没有继承_valueDeserializer.deserialize,将调用它依次运行一些 Spring 代码以将 URI 转换为Supporter.

_valueDeserializer.deserializeWithType当然,通过调用继承,普通 Jackson 期望的是一个对象,而不是 URI。

如果supporter可以为空,您可以先将支持者的 URIPOST设置为/contacts,然后将PUT支持者的 URI 设置为/contacts/xx/supporter。不幸的是我不知道任何其他解决方案。