春季启动会忽略MappedSuperClass

时间:2019-02-10 11:48:57

标签: spring rest jackson

Spring boot 2 rest api不会序列化mappingsuperclass继承。

我使用Spring Boot 2.1.2.RELEASE和PagingAndSortingRepository。 我的appconfig是:

jackson:
    serialization:
      WRITE_DATES_AS_TIMESTAMPS: false
    deserialization:
      FAIL_ON_READING_DUP_TREE_KEY: true
      ACCEPT_EMPTY_STRING_AS_NULL_OBJECT: true
    parser:
      STRICT_DUPLICATE_DETECTION: true

我的mappingsuperclass是:

@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper = false)
@MappedSuperclass
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS,
    include = JsonTypeInfo.As.PROPERTY,  property = "type", visible = true)
@JsonSubTypes({
    @JsonSubTypes.Type(value = EmployeeUnit.class, name = "EmployeeUnit")
})
public class AbstractDomain {
  ...
}

儿童班:

@Data
@NoArgsConstructor
@Entity
@Table(name = EmployeeUnit.TABLE_NAME)
@GenericGenerator(name="generator", strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator",
    parameters =  {
            @org.hibernate.annotations.Parameter(name = "sequence_name", value = "employee_unit_seq"),
            @org.hibernate.annotations.Parameter(name = "initial_value", value = "1000"),
            @org.hibernate.annotations.Parameter(name = "increment_size", value = "1")
    })
 @JsonTypeName("employeeUnit")
 public class EmployeeUnit extends AbstractDomain {
 }

输出为:

{
  "name" : "Unit-Pecs-EA1",
  "code" : "UPEA1",
  "description" : "Pecs EA Unit 1",
  "created" : "2019-02-10 12:34",
  "edited" : "2019-02-10 12:34",
  "_links" : {
     "self" : {
        "href" : "http://localhost:8480/api/v1/employeeUnit/1000"
  },
  "employeeUnit" : {
     "href" : "http://localhost:8480/api/v1/employeeUnit/1000"
  }
}

是否存在其他配置设置来强制序列化映射的超类?

1 个答案:

答案 0 :(得分:0)

我找到了解决方法:

@Configuration
public class RepositoryConfig extends RepositoryRestConfigurerAdapter {

    @Override
    public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
        config.exposeIdsFor(EmployeeUnit.class);
    }
}
相关问题