0

I am writing an online store using Spring Boot (MVC) and Hiberbate. The problem is that when I get a list of drinks, JSON gives me unnecessary information from the Page interface. I don't know how you can create an DTO for the interfaces to get rid of these fields. What should I do in this situation. Can someone have a ready-made solution?

enter image description here

    public Page<DrinkDTO> getAllDrinks(int page, int pageSize) {

        PageRequest pageRequest = PageRequest.of(page, pageSize, Sort.by("id"));

        final Page<Drink> drinks = drinkRepository.findAll(pageRequest);

        return drinkMapper.drinksToDrinksDTO(drinks);
    }
Artur Vartanyan
  • 589
  • 3
  • 10
  • 35
  • 1
    A way is write a custom `Page` for this where you can remove them [example](https://stackoverflow.com/a/63087937/4207306) – Eklavya Jul 30 '20 at 15:24
  • If you don't want the page info, don't return a page. Get the page info: `drinks.getContent()`. Then you can add any information you do want in a wrapper class. – gagarwa Jul 30 '20 at 15:46
  • @User - Upvote don't say Thanks You are the best, Man!) – Artur Vartanyan Jul 30 '20 at 16:20

2 Answers2

1
@Data
@AllArgsConstructor
public class CustomPage {

    Long totalElements;

    int totalPages;

    int number;

    int size;
}
@Data
public class PageDTO<T> {

    List<T> content;

    CustomPage customPage;

    public PageDTO(Page<T> page) {
        this.content = page.getContent();
        this.customPage = new CustomPage(page.getTotalElements(),
                page.getTotalPages(), page.getNumber(), page.getSize());
    }

Service for example:

public PageDTO<DrinkDTO> getAllDrinks(int page, int pageSize) {

        PageRequest pageRequest = PageRequest.of(page, pageSize, Sort.by("id"));

        final Page<Drink> drinks = drinkRepository.findAll(pageRequest);

        return new PageDTO<DrinkDTO>(drinkMapper.drinksToDrinksDTO(drinks));
    }
Artur Vartanyan
  • 589
  • 3
  • 10
  • 35
-1

I use native query and then I i do dto projections most of the time.

Here is an example of DTO projection

public interface OvertimeRequestView {
public Long getId();

public String getEmployeeFirstName();

public String getEmployeeLastName();

public Long getOvertimeHours();

public Date getOvertimeDate();

public String getDescription();

public String getStatus();

public String getApproverName();

public default String getEmployeeFullName() {
    String lastName = this.getEmployeeLastName();
    String firstName = this.getEmployeeFirstName();
    if (null != firstName) {
        return firstName.concat(" ").concat(lastName);
    }
    return lastName;
}

}

and here is the repository with a native query. notice that since the query returns 'id' column I have a getId() method in the dto above,and since it has employeeFirstName i have getEmployeeFirstName() in the dto and so on. Notice also that I include a count query, without a count query, the queries sometime fail especially if the queries are complex and contain joins

@Query(value = "select ovr.id,\n" +
        "       u.first_name       as employeeFirstName,\n" +
        "       u.last_name        as employeeLastName,\n" +
        "       ovr.overtime_date  as overtimeDate,\n" +
        "       ovr.description  as description,\n" +
        "       ovr.overtime_hours as overtimeHours\n" +
        "from overtime_requests  ovr\n" +
        "         left join employees e on ovr.employee_id = e.id\n" +
        "         left join users u on e.user_id = u.id",
        nativeQuery = true,
        countQuery = "select count(ovr.id)\n" +
                "from overtime_requests  ovr\n" +
                "         left join employees e on ovr.employee_id = e.id\n" +
                "         left join users u on e.user_id = u.id")
public Page<OvertimeRequestView> getAllActive(Pageable pageable);

For more you can check from spring data documentation

Emmanuel Ogoma
  • 584
  • 5
  • 12