Files
spring-boot-rest/persistence-modules/spring-data-rest-2/src/main/java/com/baeldung/serializeentityid/PersonController.java
Yavuz Tas 96dccad40c BAEL-2729 code samples for the article (#12443)
* BAEL-2729 code samples for the article

* BAEL-2729 fix formatting issues.

Co-authored-by: Yavuz Tas <ytas@vwd.com>
2022-07-15 15:34:02 +02:00

28 lines
1.0 KiB
Java

package com.baeldung.serializeentityid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.rest.webmvc.RepositoryRestController;
import org.springframework.data.web.PagedResourcesAssembler;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.PagedModel;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
@RepositoryRestController
public class PersonController {
@Autowired
PersonRepository repository;
@GetMapping("/persons")
ResponseEntity<?> persons(PagedResourcesAssembler resourcesAssembler) {
Page<Person> persons = this.repository.findAll(Pageable.ofSize(20));
Page<PersonDto> personDtos = persons.map(PersonDto::new);
PagedModel<EntityModel<PersonDto>> pagedModel = resourcesAssembler.toModel(personDtos);
return ResponseEntity.ok(pagedModel);
}
}