在将应用程序迁移到Spring Boot之后使用Spring Data Rest时,我发现使用@Id的实体属性不再编组为JSON [英] While using Spring Data Rest after migrating an app to Spring Boot, I have observed that entity properties with @Id are no longer marshalled to JSON

查看:238
本文介绍了在将应用程序迁移到Spring Boot之后使用Spring Data Rest时,我发现使用@Id的实体属性不再编组为JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题与这个问题有关( Spring boot @ResponseBody doesn' t序列化实体ID )。我观察到,在将应用程序迁移到Spring Boot并使用spring-boot-starter-data-rest依赖项后,我的实体@Id字段不再在生成的JSON中编组。

This question is related to this SO question (Spring boot @ResponseBody doesn't serialize entity id). I have observed that after migrating an app to Spring Boot and using the spring-boot-starter-data-rest dependency, my entity @Id fields are no longer marshalled in the resulting JSON.

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

This is my request mapping and while debugging, I can see the data isn't being changed prior to returning it, so the @Id properties are being stripped later on.

@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,因为我的应用程序允许用户按该值搜索。

Is there a setting that would allow me to keep the @Id field in the resulting JSON because my app allows the user to search by that value.

谢谢:)

推荐答案

默认情况下,Spring Data Rest不会吐出ID。但是,您可以通过选择性启用它/rest/core/config/RepositoryRestConfiguration.html#exposeIdsFor(java.lang.Class ...)rel =noreferrer> exposeIdsFor(..)方法。您可以在配置中执行此操作,如下所示

By default Spring Data Rest does not spit out IDs. However you can selectively enable it through exposeIdsFor(..) method. You could do this in configuration, something like this

@Configuration
public static class RepositoryConfig extends
        RepositoryRestMvcConfiguration {

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

这篇关于在将应用程序迁移到Spring Boot之后使用Spring Data Rest时,我发现使用@Id的实体属性不再编组为JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆