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

时间:2015-03-12 22:10:47

标签: 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;
...
}

PersonSupporter

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

CompanySupporter

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

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

3 个答案:

答案 0 :(得分:3)

使用RelProvider

的另一种解决方法
  1. 请勿使用@JsonTypeInfo
  2. RelProvider子类

    创建SupporterEntity
    @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);
      }
    }
    
  3. 另见:

答案 1 :(得分:1)

看起来像杰克逊的问题。具体而言,它是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支持者的/contacts/xx/supporter URI。不幸的是,我不知道任何其他解决方案。

答案 2 :(得分:0)

您应该能够通过在属性/方法级别设置@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;
}