【发布时间】:2017-01-15 11:27:47
【问题描述】:
不管有没有这个注解,我的 JPA 上都有一个属性@Entity
@Entity
public class Myentity extends ResourceSupport implements Serializable {
...
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name="idrepository")
@JsonInclude(JsonInclude.Include.ALWAYS)
private MyentitySource entitysource;
...
}
当我返回时没有被映射:
@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 hatoas 项目,我使用完全相同的实体、存储库、控制器和汇编器实现,在我的 pom 上具有完全相同的依赖项和版本,以及非常相似的配置(我是没有定义任何特殊的 jackson 映射器或任何东西,只使用默认的 rest/hateoas 配置),它确实在那里工作:MyentitySource entitysource 属性,它是另一个扩展 ResourceSupport 的 JPA 实体,被序列化并包含进入 JSON 输出。
我已经玩了几个小时了,但我很迷茫。我已经验证了这种行为在两个应用程序中的整个应用程序中都发生了:在任何 @Entity 上定义的 @ManyToOne 关系被映射并出现在一个应用程序的 JSON 输出中,但不在另一个应用程序中。
如何让这些字段显示在 JSON 输出中?
【问题讨论】:
-
当我尝试重现您的问题时,情况有所不同。我必须使用
@JsonInclude(value=Include.ALWAYS)而不是@JsonInclude(JsonInclude.Include.ALWAYS)。不要认为这是问题,但您可以尝试一下。 -
没有区别。使用 Eclipse 调试器我可以看到有一个
org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration$ResourceSupportHttpMessageConverter其ObjectMapper有一个BeanSerializerFactory有一个SerializerFactoryConfig有一个org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module$AssociationOmittingSerializerModifier(这是一个BeanSerializerModifier),我相信修饰符搞砸了我的 JPA@ManyToOne协会。 -
我现在将尝试编写自己的
RepositoryRestMvcConfiguration并覆盖或修改转换器以使用ObjectMapper,其序列化程序工厂配置不包含此修饰符。
标签: spring-data-rest