【问题标题】:How to reference an entity with inheritance in Spring Data REST when POSTing new entity?发布新实体时如何在 Spring Data REST 中引用具有继承的实体?
【发布时间】:2015-05-15 06:50:35
【问题描述】:

我有具有联合继承的实体:

支持者

@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;
...
}

个人支持者

@Entity
@DiscriminatorValue("PERSON")
@Table(name = "person_supporter")
public class PersonSupporterEntity extends SupporterEntity {
...
}

公司支持者

@Entity
@DiscriminatorValue("COMPANY")
@Table(name = "company_supporter")
public class CompanySupporterEntity extends SupporterEntity {
...
}

我有另一个引用 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;
    }
...
}

存储库

@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);
}
@Transactional
@RepositoryRestResource(collectionResourceRel = "person_supporters", path = "person_supporters")
public interface PersonSupporterEntityRepository extends JpaRepository<PersonSupporterEntity, Long> {

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

}
@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);
}

我使用 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
}

我得到了这个例外:

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]

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

{
   "supporter":{
      "supporterType":"PERSON",
      "id":"52"
   },
   "postcode":"1111",
   "city":"Test City 1",
   "address":"Test Address 1",
   "email":"test1@email.com",
   "newsletter":true
}

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

【问题讨论】:

  • @zeroflagL 我添加了存储库和关系
  • 顺便说一句,最好从存储库接口中删除@Transactional,首先,因为它是一个接口,其次,因为服务层应该是事务的主人。在自定义 repo 实现中,如果需要更好的性能,我建议使用@Transactional(propagation=SUPPORTS) 来启用非事务性读取。见stackoverflow.com/questions/3120143/…

标签: rest jackson spring-data-rest


【解决方案1】:

使用RelProvider 的另一种解决方法:

  1. 不要使用@JsonTypeInfo
  2. SupporterEntity 子类创建RelProvider

    @Component
    @Order(Ordered.HIGHEST_PRECEDENCE)
    public class SupporterEntityRelProvider implements RelProvider {
    
      @Override
      public String getCollectionResourceRelFor(final Class<?> type) {
        return "supporters";
      }
    
      @Override
      public String getItemResourceRelFor(final Class<?> type) {
        return "supporter";
      }
    
      @Override
      public boolean supports(final Class<?> delimiter) {
        return org.apache.commons.lang3.ClassUtils.isAssignable(delimiter, SupporterEntity.class);
      }
    }
    

另见:

【讨论】:

    【解决方案2】:

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

    if (_valueTypeDeserializer != null) {
       return _valueDeserializer.deserializeWithType(jp, ctxt, _valueTypeDeserializer);
    }
    return _valueDeserializer.deserialize(jp, ctxt);
    

    如果没有继承 _valueDeserializer.deserialize 将被调用,然后运行一些 Spring 代码将 URI 转换为 Supporter

    通过继承 _valueDeserializer.deserializeWithType 被调用,而 vanilla Jackson 当然需要一个对象,而不是 URI。

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

    【讨论】:

    • 我不认为这是杰克逊的问题。好老杰克逊工作得很好。这是来自 Spring DATA REST 的自定义 Jackson URL 序列化程序的问题。似乎这个用例还没有涵盖,但有一些解决方法。
    • @aux 在反序列化请求正文时出现问题,那么“自定义Jackson URL 序列化器”到底有什么用呢?
    • 抱歉,我的拼写错误,我的意思是 deserializer,当然。顺便说一句,这已经在 Spring Data REST 2.4 中修复 - 请参阅 jira.spring.io/browse/DATAREST-662
    【解决方案3】:

    您应该能够通过在属性/方法级别设置 @JsonTypeInfo(use= JsonTypeInfo.Id.NONE) 来解决此问题,例如

    试试这个:

    @ManyToOne // same error with @OneToOne
    @JoinColumn(name = "supporter_id", referencedColumnName = "id", nullable = false)
    @JsonTypeInfo(use= JsonTypeInfo.Id.NONE)
    public SupporterEntity getSupporter() {
        return supporter;
    }
    

    【讨论】:

      猜你喜欢
      • 2014-07-09
      • 2017-11-13
      • 1970-01-01
      • 2020-09-22
      • 2021-08-06
      • 2014-08-25
      • 2015-02-09
      • 1970-01-01
      相关资源
      最近更新 更多