Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
430 views
in Technique[技术] by (71.8m points)

java - 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

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.

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;
}

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.

Thanks :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...