SPRING PAGINATION in MOCK API

Asked

Viewed 33 times

0

I am doing a Java API, and implemented the pagination, the result of JSON pagination is as follows:

{
  "content": [
    {
      (AQUI TEM OS RESULTADOS DA QUERY)
    }
  ],
  "empty": true,
  "first": true,
  "last": true,
  "number": 0,
  "numberOfElements": 0,
  "pageable": {
    "offset": 0,
    "pageNumber": 0,
    "pageSize": 0,
    "paged": true,
    "sort": {
      "empty": true,
      "sorted": true,
      "unsorted": true
    },
    "unpaged": true
  },
  "size": 0,
  "sort": {
    "empty": true,
    "sorted": true,
    "unsorted": true
  },
  "totalElements": 0,
  "totalPages": 0
}

I also have another class called "Baseretorno" that has the same items as pagination, but in English:

{
  "itens": [
    {
      (AQUI ERA PARA VIR O RESULTADO DA CONSULTA)
    }
  ],
  "paginacao": {
    "pagina": 0,
    "tamanhoPagina": 0,
    "totalPaginas": 0,
    "totalItens": 0
  }
}

Basically, I would like to make the pagination by my class, so that it was displayed in Portuguese, I could not rename and nor modify the values of the "content" nor the other attributes of the Spring pagination. Can anyone help me with this, how could I make my class act the same, or pick up page values? Thanks.

1 answer

1

I’m not sure I understand what you want.

First point, it would not be a good practice to use a class of own page, I recommend to use the page provided by Spring.

Second point, since you did not provide the classes of your project I will assume that you are following the common practices of Spring, and for example I will use a class of my own.

first you will need a class that stores pagination information.

public class Paginacao {

    private int pagina;
    private int tamanhoPagina;
    private int totalPaginas;
    private long totalItens;

    public Paginacao(int pagina, int tamanhoPagina, int totalPaginas, long totalItens) {
        this.pagina = pagina;
        this.tamanhoPagina = tamanhoPagina;
        this.totalPaginas = totalPaginas;
        this.totalItens = totalItens;
    }
//getter, setter e contrutor sem args.
}

Note that there is a constructor with parameters.

now let’s create the class that will actually be the page

public class PaginaEmPortugues {

private List<Book> itens;
private Paginacao paginacao;

public PaginaEmPortugues(Page<Book> page) {
    this.itens = page.getContent();
    this.paginacao = new Paginacao(
            page.getPageable().getPageNumber(),
            page.getPageable().getPageSize(),
            page.getTotalPages(),
            page.getTotalElements()
    );
//getter, setter e contrutor sem args.
}

Note that we used the previous class constructor in this class constructor.

and finally the method in the controller:

@GetMapping
private PaginaEmPortugues readAll(@PageableDefault(sort="nome", direction = Sort.Direction.ASC, page = 0, size = 20) Pageable pageable){

    Page<Book> englishPage = bookRepository.findAll(pageable);
    return new PaginaEmPortugues(englishPage);
}

I hope you understand my code, another point, is that if you want to use this page for several different classes, you should do a search on Generics and then implement in this class.

To prove that it works I tested.

{
"itens": [
    {
        "id": 3,
        "nome": "Descrição"
    },
    {
        "id": 1,
        "nome": "Descrição"
    },
    {
        "id": 2,
        "nome": "Descrição"
    }
],
"paginacao": {
    "pagina": 0,
    "tamanhoPagina": 20,
    "totalPaginas": 1,
    "totalItens": 3
}

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.