存储库GET返回同时包含内容对象和内容数组的JSON

时间:2018-07-19 05:43:32

标签: spring-boot spring-data-jpa spring-data-rest

我有一个具有ManyToOne关系的实体,当我使用restTemplate.getForEntity()时,嵌套实体的值包装在content字段中,同时添加了一个content数组覆盖

@ManyToOne(fetch = FetchType.LAZY)
@RestResource(exported = false)
@JoinColumn(name = "namespace", nullable = false)
private Namespace namespace;

具有这种关系的实体上的GET返回以下输出主体

{
  "id" : "some_containing_id",
  "alertDefinition" : null,
  "namespace" : {
    "content" : {
      "id" : "some_namespace_id",
      "emailSenderName" : "some sender",
      "emailSenderId" : "foo@bar.com",
      "createdAt" : "2018-07-19T05:24:04.473Z",
      "updatedAt" : "2018-07-19T05:24:04.473Z"
    },
    "content" : [ ],
    "links" : [ ]
  },
  ...

因此,要对包含2个内容字段的名称空间进行序列化,用数组替换包含值的内容对象

SpringBoot 2.0.3.RELEASE

Namespace.java

@Entity
@Table(name = "namespace"
    , schema = "alert_notification"
)
public class Namespace implements java.io.Serializable {
    public transient static final String
        EMAIL_SENDER_NAME_DEFAULT = "some sender";
    public transient static final String
        EMAIL_SENDER_ID_DEFAULT = "foo@vbar.com";

    @Id
    @Column(name = "id", unique = true, nullable = false)
    private String id;

    @Builder.Default
    @Column(name = "email_sender_name")
    private String emailSenderName = EMAIL_SENDER_NAME_DEFAULT;

    @Builder.Default
    @Column(name = "email_sender_id")
    private String emailSenderId = EMAIL_SENDER_ID_DEFAULT;

    @CreationTimestamp
    @Column(name = "created_at", nullable = false, updatable = false)
    private OffsetDateTime createdAt;

    @UpdateTimestamp
    @Column(name = "updated_at", nullable = false)
    private OffsetDateTime updatedAt;

}

1 个答案:

答案 0 :(得分:0)

由于某种原因,该问题是由最近引入的Hibernate5中包含的com.fasterxml.jackson.datatype:jackson-datatype-hibernate5模块引起的。

有趣的是,甚至不需要启用该模块,仅依赖于类路径会导致问题。