此問題與此SO問題有關(Spring boot @ResponseBody doesn't serialize entity id)。我觀察到,在將應用程序遷移到Spring Boot並使用spring-boot-starter-data-rest依賴項後,我的實體@Id字段不再在生成的JSON中編組。將應用程序遷移到Spring Boot後使用Spring 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,因爲我的應用程序允許用戶通過該值來搜索設置。
謝謝:)
它應該是注意到如果你使用的是spring-boot,你需要擴展'SpringBootRepositoryRestMvcConfiguration'而不是'RepositoryRestMvcConfiguration',並且使它成爲一個普通的類而不是靜態類。 – jax
從版本2.4開始,'configureRepositoryRestConfiguration'方法應該通過擴展'RepositoryRestConfigurerAdapter'類來覆蓋。 –
別忘了現在支持getter和setter爲實體類中的id字段!(我忘了它,並且正在爲此尋找很多時間) – flipperweid