我有一个工作API,我需要所有的CRUD方法,但我也有一个模拟客户端,当我的分页GET方法被调用时,它会抛出我和异常
这是原始API上的工作控制器:
@RestController
@RequestMapping("/cargos")
public class CargoController {
@Autowired
private CargoService cargoService;
// ACHAR TODOS
@GetMapping
public Page<Cargo> consultar(Pageable paginacao) {
return cargoService.consultar(paginacao);
}
}
这是原始API上的服务:
@Service
public class CargoService {
@Autowired
private CargoRepositorio repositoryCargos;
// BUSCA TODOS
public Page<Cargo> consultar(Pageable paginacao) {
return repositoryCargos.findAll(paginacao);
}
}
这一切都正常,但在飞行客户端中,每次调用get方法时,它都会抛出并异常:
catch(InvocationTargetException ex){反射Utils. rethrowRuntimeException(ex.getTargetException());
这是我的假装客户,我得到了一个服务:
@FeignClient(url="http://localhost:8080/cargos",name="cargo")
public interface CargoFeign {
//BUSCA TODOS
@GetMapping
Page<Cargo> consultar(Pageable paginacao);
和调度程序:
@Component
@Slf4j
public class CargoScheduler {
@Autowired
private CargoFeign cargoFeign;
@Scheduled(cron = "0/1 * * * * *")
public void executar() {
log.debug("executando");
// BUSCANDO TODOS OS CARGOS
Pageable paginacao = PageRequest.of(0, 10, Sort.by( Order.asc("id")));
Page<Cargo> cargo2 = cargoFeign.consultar(paginacao);
System.out.println("Listando Cargos");
System.out.println(cargo2);
}
您可以使用Spring HATEOAS提供的资源或资源。您需要在客户端上添加Spring HATEOAS依赖项:
compile('org.springframework.boot:spring-boot-starter-hateoas')
在您的主类中启用Spring Boot的超媒体支持:
@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
并更改您的假装客户端:
@FeignClient(url="http://localhost:8080/cargos",name="cargo")
public interface CargoFeign {
//BUSCA TODOS
@GetMapping
Resources<Cargo> consultar(Pageable paginacao);
澄清一下这个回答有点帮助
Spring Data在模拟客户端中不支持作为Request estParam分页
这就是我的假客户现在的样子
//BUSCA TODOS
@GetMapping("/pagina/{paginaAtual}/tamanho/{tamanho}")
Page<Cargo> findAll(@PathVariable("paginaAtual") Integer paginaAtual, @PathVariable("tamanho") Integer tamanho);