Bom dia galera. Estou tentando fazer a paginação de dados que estou consumindo da API pública do GitHub. São apenas 4 dados, name, image, stars e forks;
Acontece que a lista de “content” só fica vazia… Já tentei 3 métodos mas não consegui.
Eu sei que existem outras formas de se fazer paginação, mas fui desafiado a faze no back-end.
Abaixo vou mostrar as classes envolvidas nisso:
package com.buscarepositorio.buscador.controllers;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.buscarepositorio.buscador.resources.BaseController;
@RestController
@RequestMapping("/repositorios")
public class RepositoriosController extends BaseController {
/*
* @Autowired private BaseService service;
*/
}
BaseService:
package com.buscarepositorio.buscador.resources;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;
import com.buscarepositorio.buscador.entities.RepositorioEntity;
import com.buscarepositorio.buscador.repositories.RepositoriosRepository;
import com.buscarepositorio.buscador.services.RepositoriosResponseApi;
@Service
public abstract class BaseService {
@Autowired
private RepositoriosRepository repository;
//@Autowired
// RestTemplate restTemplate;
public Page<RepositorioEntity> getAll(Pageable pageable) {
return repository.findAll(pageable ); }
public RepositorioEntity salvar(RepositorioEntity entity) {
return repository.save(entity);
}
public List<RepositorioEntity> salvarRepositorios (RepositoriosResponseApi res){
List<RepositorioEntity> lista = new ArrayList<>();
res.getItems().forEach(item -> {
RepositorioEntity repositorio = new RepositorioEntity();
repositorio.setForks(item.getForks_count());
repositorio.setImage(item.getOwner().getAvatar_url());
repositorio.setStars(item.getStargazers_count());
repositorio.setName(item.getName());
lista.add(repository.save(repositorio));
});
return lista;
}
public RepositorioEntity getOne(Integer id) {
Optional<RepositorioEntity> optional = repository.findById(id);
if (!optional.isPresent()) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Entidade não foi encontrada!");
}
RepositorioEntity entity = optional.get();
return entity;
}
public Page<RepositorioEntity> getAllPageable(RepositorioEntity filter, Pageable pageable){
ExampleMatcher matcher = ExampleMatcher
.matching()
.withIgnoreNullValues()
.withIgnoreCase()
.withStringMatcher(
ExampleMatcher.StringMatcher.CONTAINING);
Example<RepositorioEntity> example = Example.of(filter, matcher);
return repository.findAll(example, pageable);
}
public List<RepositorioEntity> getAll() {
return repository.findAll();
}
}
BaseController:
package com.buscarepositorio.buscador.resources;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.client.RestTemplate;
import com.buscarepositorio.buscador.entities.RepositorioEntity;
import com.buscarepositorio.buscador.services.RepositoriosResponseApi;
public abstract class BaseController extends BaseService {
@Autowired
private BaseService service;
private RestTemplate template = new RestTemplate();
private String baseUrl = "https://api.github.com/search/repositories?q=language:java&sort=stars&page=1";
@GetMapping
public ResponseEntity<?> getRepositorios() {
RepositoriosResponseApi response = template.getForEntity(baseUrl, RepositoriosResponseApi.class).getBody();
return new ResponseEntity<>(service.salvarRepositorios(response), HttpStatus.OK);
}
@GetMapping("/pageable") // Listar todas entidades com paginação public
ResponseEntity<Page<RepositorioEntity>> index(RepositorioEntity filter, @PageableDefault
Pageable pageable) {
Page<RepositorioEntity> entities = service.getAllPageable(filter, pageable);
return ResponseEntity.ok(entities);
}
}
BaseRepository:
package com.buscarepositorio.buscador.resources;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.buscarepositorio.buscador.entities.RepositorioEntity;
@Repository
public abstract interface BaseRepository extends JpaRepository<RepositorioEntity, Integer>{
}
RepositorioEntity:
package com.buscarepositorio.buscador.entities;
import javax.persistence.Entity;
import javax.persistence.Table;
import com.buscarepositorio.buscador.resources.BaseEntity;
//ENTIDADE DO BANCO DE DADOS
@Entity
@Table(name = "repositorios")
public class RepositorioEntity extends BaseEntity {
private String image;
private String name;
private int stars;
private int forks;
public RepositorioEntity(String image, String name, int stars, int forks) {
super();
this.image = image;
this.name = name;
this.stars = stars;
this.forks = forks;
}
public RepositorioEntity() {
// TODO Auto-generated constructor stub
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getStars() {
return stars;
}
public void setStars(int stars) {
this.stars = stars;
}
public int getForks() {
return forks;
}
public void setForks(int forks) {
this.forks = forks;
}
}
Repositorios:
package com.buscarepositorio.buscador.entities;
//ENTIDADE QUE RECEBE A RESPOSTA DA API ANTES DE PERSISTIR NO BANCO
public class Repositorios {
private String name;
private int forks_count;
private int stargazers_count;
private RepositoriosOwner repositoriosOwner;
/*
*
* { "name": "CS-Notes", "owner": { "avatar_url":
* "https://avatars3.githubusercontent.com/u/36260787?v=4", },
* "stargazers_count": 115510,
*
* "forks_count": 37607,
*
* }
*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getForks_count() {
return forks_count;
}
public void setForks_count(int forks_count) {
this.forks_count = forks_count;
}
public int getStargazers_count() {
return stargazers_count;
}
public void setStargazers_count(int stargazers_count) {
this.stargazers_count = stargazers_count;
}
public RepositoriosOwner getOwner() {
return repositoriosOwner;
}
public void setOwner(RepositoriosOwner repositoriosOwner) {
this.repositoriosOwner = repositoriosOwner;
}
}
RepositoriosOwner:
package com.buscarepositorio.buscador.entities;
public class RepositoriosOwner {
private String avatar_url;
public String getAvatar_url() {
return avatar_url;
}
public void setAvatar_url(String avatar_url) {
this.avatar_url = avatar_url;
}
}