【问题标题】:Spring boot ignores JSonTypeInfo in MappedSuperClassSpring boot 忽略 MappedSuperClass 中的 JSonTypeInfo
【发布时间】:2019-02-10 11:48:56
【问题描述】:

Spring boot 2 rest api 不序列化映射超类继承。

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

我的映射超类是:

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

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

【问题讨论】:

    标签: spring rest jackson


    【解决方案1】:

    我找到了解决办法:

    @Configuration
    public class RepositoryConfig extends RepositoryRestConfigurerAdapter {
    
        @Override
        public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
            config.exposeIdsFor(EmployeeUnit.class);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-04
      • 1970-01-01
      • 2016-08-01
      • 1970-01-01
      • 2017-05-04
      • 2015-08-03
      • 2023-03-15
      • 2016-08-19
      相关资源
      最近更新 更多