Spring 数据剩余忽略@JsonInclude 注解
Spring Data Rest ignoring @JsonInclude annotation
无论有没有这个注释,我的 JPA 上都有一个 属性 @Entity
@Entity
public class Myentity extends ResourceSupport implements Serializable {
...
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name="idrepository")
@JsonInclude(JsonInclude.Include.ALWAYS)
private MyentitySource entitysource;
...
}
当我 return:
时没有被映射
@RequestMapping("/myentity/{uuid}")
public ResponseEntity<Myentity> getResourceById(@PathVariable("uuid") UUID uuid) {
Myentity result = myentityRepository.findOne(uuid);
return ResponseEntity.ok(myentityAssembler.toResource(result));
}
myentityAssembler.toResource(result)
确实包含这个MyentitySource entitysource
,但是JSON输出没有。
最奇怪的是我有另一个 spring boot hateoas 项目,我在其中使用完全相同的实体、存储库、控制器和汇编程序实现,在我的 pom 上具有完全相同的依赖项和版本,以及非常相似的配置(我没有定义任何特殊的 jackson 映射器或任何东西,只是使用默认的 rest/hateoas 配置),它在那里工作: MyentitySource entitysource
属性,这是另一个扩展 ResourceSupport 的 JPA 实体,被序列化并包含在 JSON 输出中。
我已经花了几个小时了,但我很迷茫。我已验证此行为在两个应用程序的整个应用程序中都在发生:在任何 @Entity
上定义的 @ManyToOne
关系被映射并出现在一个应用程序的 JSON 输出中,但不在其他。
如何让这些字段显示在 JSON 输出中?
如果 MyentitySource
不是导出的实体,则将包含 entitysource
。如果它是一个——这里似乎是这种情况——那么将它包括在内将是错误的。包括关联可能导致将整个数据库发送给客户端。此外,它是具有自己的 URI 的独立资源。因此,该 URI 的 link 包含在响应中。
CascadeType.ALL
意味着 Myentity
是一个聚合,因此首先不应导出 MyentitySource
。那会解决你的问题。如果我的假设是错误的,那么您仍然可以使用投影来获得 entitysource
。我可以推荐给你 this answer from Spring's Oliver Gierke and the relevant chapter of the documentation.
无论有没有这个注释,我的 JPA 上都有一个 属性 @Entity
@Entity
public class Myentity extends ResourceSupport implements Serializable {
...
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name="idrepository")
@JsonInclude(JsonInclude.Include.ALWAYS)
private MyentitySource entitysource;
...
}
当我 return:
时没有被映射@RequestMapping("/myentity/{uuid}")
public ResponseEntity<Myentity> getResourceById(@PathVariable("uuid") UUID uuid) {
Myentity result = myentityRepository.findOne(uuid);
return ResponseEntity.ok(myentityAssembler.toResource(result));
}
myentityAssembler.toResource(result)
确实包含这个MyentitySource entitysource
,但是JSON输出没有。
最奇怪的是我有另一个 spring boot hateoas 项目,我在其中使用完全相同的实体、存储库、控制器和汇编程序实现,在我的 pom 上具有完全相同的依赖项和版本,以及非常相似的配置(我没有定义任何特殊的 jackson 映射器或任何东西,只是使用默认的 rest/hateoas 配置),它在那里工作: MyentitySource entitysource
属性,这是另一个扩展 ResourceSupport 的 JPA 实体,被序列化并包含在 JSON 输出中。
我已经花了几个小时了,但我很迷茫。我已验证此行为在两个应用程序的整个应用程序中都在发生:在任何 @Entity
上定义的 @ManyToOne
关系被映射并出现在一个应用程序的 JSON 输出中,但不在其他。
如何让这些字段显示在 JSON 输出中?
MyentitySource
不是导出的实体,则将包含 entitysource
。如果它是一个——这里似乎是这种情况——那么将它包括在内将是错误的。包括关联可能导致将整个数据库发送给客户端。此外,它是具有自己的 URI 的独立资源。因此,该 URI 的 link 包含在响应中。
CascadeType.ALL
意味着 Myentity
是一个聚合,因此首先不应导出 MyentitySource
。那会解决你的问题。如果我的假设是错误的,那么您仍然可以使用投影来获得 entitysource
。我可以推荐给你 this answer from Spring's Oliver Gierke and the relevant chapter of the documentation.