0

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?

1 Answers1

0

Use this:

Page<User> pages = userService.getPage(PageRequest.of(page, size));
Page<UserDto> pagesDto = page.map(UserDto::new);
Miroslav Trninic
  • 3,327
  • 4
  • 28
  • 51
aang13
  • 389
  • 1
  • 3
  • 14
  • Please share your BookDto class. – aang13 Jun 03 '21 at 18:28
  • Data Builder NoArgsConstructor AllArgsConstructor @JsonInclude(JsonInclude.Include.NON_NULL) public class UserDto { private Long id; private String firstName; private String lastName; private String email; private String password; } – Jakub Kołacz Jun 03 '21 at 20:32
  • I did brainstorm again and found correct implementation to this solution. I have defined method in mapper interface and implemented it. This method uses second line of your code. Thanks – Jakub Kołacz Jun 04 '21 at 07:30