darrachequesne / spring-data-jpa-datatables

Spring Data JPA extension to work with the great jQuery plugin DataTables (https://datatables.net/)
Apache License 2.0
448 stars 172 forks source link

How to Work with UserDTO #140

Closed Vaibhav-Andhale closed 2 years ago

Vaibhav-Andhale commented 2 years ago

i'm going through your document and i found below code but what is "toUserDTO". Please explain. @RestController public class UserRestController {

@Autowired private UserRepository userRepository;

@RequestMapping(value = "/data/users", method = RequestMethod.GET) public DataTablesOutput getUsers(@Valid DataTablesInput input) { return userRepository.findAll(input, toUserDTO); } }

darrachequesne commented 2 years ago

The toUserDTO() method maps the User instance to a UserDTO instance, either with:

  public UserDTO toUserDTO(User user) {
    UserDTO dto = new UserDTO();
    dto.setId(user.getId());
    // ...
    return dto;
  }
Vaibhav-Andhale commented 2 years ago

Hi @darrachequesne , Thanks for response. I did something like you.

public Function<User, UserDTO> createConverter(){ return (Function<User, UserDTO>) (input) ->{ UserDTO dto= new UserDTO(); dto.setId(input.getId)
//... return dto; }; }