Search code examples
javaspring-bootrestdto

Spring Boot REST API DTO method


I am creating simple rest api using springboot. I am modifing my methods because I need them to be build using DTO. Here's code:

private final UserService userService;
private final UserMapper userMapper;

@GetMapping
public Page<UserDto> getUserPage(@RequestParam int page,@RequestParam int size){
    return userService.getPage(PageRequest.of(page, size));
}

I want function to return page of userDto (as in method header). How to change return phrase to have userDto page returned instead of user page?


Solution

  • Use this:

    Page<User> pages = userService.getPage(PageRequest.of(page, size));
    Page<UserDto> pagesDto = page.map(UserDto::new);