小编典典

在将应用程序迁移到Spring Boot后使用Spring Data Rest时,我注意到带有@Id的实体属性不再编组为JSON

json

这个问题与此SO问题有关(Springboot@ResponseBody不会序列化实体id)。我观察到,将应用程序迁移到Spring
Boot并使用spring-boot-starter-data-rest依赖关系后,我的实体@Id字段不再编组到结果JSON中。

这是我的请求映射,在调试时,我可以看到数据在返回之前没有被更改,因此稍后会删除@Id属性。

@RequestMapping(method = RequestMethod.GET, produces = {"application/json"})
public PagedResources<Receipt> receipts(Pageable pageable, PagedResourcesAssembler assembler) {
    Page<Receipt> receipts = receiptRepository.findByStorerAndCreatedDateGreaterThanEqual("003845", createdStartDate, pageable);
    PagedResources<Receipt> pagedResources = assembler.toResource(receipts, receiptResourceAssembler);
    return pagedResources;
}

是否有一个设置可以让我将@Id字段保留在结果JSON中,因为我的应用程序允许用户按该值进行搜索。

谢谢 :)


阅读 261

收藏
2020-07-27

共1个答案

小编典典

默认情况下,Spring Data
Rest不吐出ID。但是,您可以通过ExposureIdsFor(..)方法
有选择地 启用它。您可以在配置中执行以下操作

@Configuration
public static class RepositoryConfig extends
        RepositoryRestMvcConfiguration {

    @Override
    protected void configureRepositoryRestConfiguration(
            RepositoryRestConfiguration config) {
        config.exposeIdsFor(Class1.class, Class2.class);
    }
}
2020-07-27