Spring Rest Data hateotas pagination (Problems with next and previous button)

0

I have managed to create a simple pagination but I do not know how to add the typical Next , Previous and the total of pages that there are.

I will try to leave the code I have, I use the MVC Pattern although there is an intermediate class in which I remove all the controller logic.

This is the model

@Entity
@Table(name = "url")
public class URL {

    @Id
    @GeneratedValue
    private Long id;

    @Column(unique = true,name="url")
    private String url;

    @Column(columnDefinition = "TINYINT")
    @Type(type = "org.hibernate.type.NumericBooleanType")
    private boolean hasOpen;

    private Date fechaVisita;

    public URL() {
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public Date getFechaVisita() {
        return fechaVisita;
    }

    public void setFechaVisita(Date fechaVisita) {
        this.fechaVisita = fechaVisita;
    }

    public boolean isHasOpen() {
        return hasOpen;
    }

    public void setHasOpen(boolean hasOpen) {
        this.hasOpen = hasOpen;
    }


}

The first step is to create the interface.

interface URLPaginationRepository extends PagingAndSortingRepository<URL,Integer> {

}

Then I have to implement the service.

@Service
public class UrlService implements IUrlService {

    @Autowired
    private URLPaginationRepository urlRepository;

    @Override
    public Page<URL> listAllByPage(Pageable pageable) {
        return personRepository.findAll(pageable);
    }
}

Then we create an intermediate class between the controller and the service.

@Component
public class GetUrlPaginatedMethod {
    @Autowired
    UrlService urlService;

    public Page<URL> list(Pageable pageable) {
        return urlService.listAllByPage(pageable);
    }

}

And finally I have the driver

@Controller
public class GetUrlPaginatedController {

   @Autowired
   GetUrlPaginatedMethod getUrlPaginatedMethod;

    @GetMapping(value="/urls")
    ResponseEntity<Page<URL>> list(Pageable pageable){
        Page<URL> persons = getUrlPaginatedMethod.list(pageable) ;
        return ResponseEntity.ok(persons);
    }
}

Well, finally when I make the request to the corresponding url ( urls? page = 0 & size = 2 & sort = id, DESC ) it returns me the next JSON response.

{
    "content": [
        {
            "id": 2985,
            "url": "https://www.mismarcadores.com/baloncesto/europa/eurochallenge-2005-2006/",
            "hasOpen": false,
            "fechaVisita": null
        },
        {
            "id": 2984,
            "url": "https://www.mismarcadores.com/baloncesto/europa/eurochallenge-2006-2007/",
            "hasOpen": false,
            "fechaVisita": null
        }
    ],
    "last": false,
    "totalElements": 2985,
    "totalPages": 1493,
    "size": 2,
    "number": 0,
    "sort": [
        {
            "direction": "DESC",
            "property": "id",
            "ignoreCase": false,
            "nullHandling": "NATIVE",
            "ascending": false
        }
    ],
    "numberOfElements": 2,
    "first": true
}

This is correct, so it works correctly but I do not know how to make the typical Next, Previous buttons and the remaining pages.

I have this class created that may be helpful but I do not know how to implement it.

public class PageOffsetUtils {

    public static int calculatePreviousPageOffset(Integer offset, Integer limit){
        return Math.max(offset-limit, 0);
    }

    public static int calculateLastPageOffset(Integer limit, Integer total){
        int pageTmp = (int) Math.ceil((double)total / limit);
        return Math.max(pageTmp-1,0)*limit;
    }

    public static int calculateNextPageOffset(Integer offset, Integer limit, Integer total){
        int nextPageOffset = offset + limit;
        if(nextPageOffset < total){
            return nextPageOffset;
        } else {
            return calculateLastPageOffset(limit, total);
        }
    }

    public static int calculateNextPageOffset(Integer offset, Integer limit){
        return offset + limit;
    }
} 

Any ideas about it?

    
asked by kiko 08.05.2018 в 22:32
source

0 answers