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

Patrick Grimard picture Patrick Grimard · Jul 24, 2014 · Viewed 9.1k times · Source

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 :)

Answer

Stackee007 picture Stackee007 · Jul 24, 2014

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