Search code examples
javaspring-bootrestmicroservicesresttemplate

Rest Template request aborted by the software in your host machine


I'm trying to hit an endpoint in another service registered in my Eureka Server called "user-service", using rest template. The endpoint in the other service will return all the data from the database, as you can see here:

@ResponseBody
@RequestMapping(method = RequestMethod.GET)
@ResponseStatus(value = HttpStatus.OK)
public Page<User> findAll() {
    return userRepository.findAll();
}

My "client-service" is trying to use the "user-service"'s endpoint with this code. UserClient:

public List<UsuarioVo> getUsers(){
    HttpEntity headers = new HttpEntity(getHeaders());
    String url = urlUserClient + "/users";
    ResponseEntity<List<UserVo>> result= restTemplate.exchange(url, HttpMethod.GET, headers , new ParameterizedTypeReference<List<UserVo>>() {});
    return new ObjectMapper().convertValue(result.getBody(), TypeFactory.defaultInstance().constructCollectionType(List.class, UserVo.class));
}

private HttpHeaders getHeaders() {
    HttpHeaders headers = new HttpHeaders();
    headers.set("username", request.getHeader("username"));
    return headers;
}

ClientService:

@ResponseBody
@RequestMapping(value = "/users", method = RequestMethod.GET)
@ResponseStatus(value = HttpStatus.OK)
@PreAuthorize("@searchRole.hasPermission('GET')")
public ResponseEntity<List<ProdutividadeDTO>> getProdutividade(){
    List<UserVo> users = pessoaServiceClient.obterUsuarios();
}

When I hit the enpoint, my "user-service" throw an exception after executing all the sql codes: org.apache.catalina.connector.ClientAbortException: java.io.IOException: An established connection was aborted by the software in your host machine... Caused by: java.io.IOException: An established connection was aborted by the software in your host machine Someone knows why this is happening?


Solution

  • Locally I fixed! The thing is, in my project we use a super class called OperationService where we have all the generic GET, POST, PUT, PATCH and DELETE calls. I was calling the GET without any especific URL eg: "users/loadall", when I build up an endpoint called "loadAll" with this URL and hit it, in my local configuration it worked perfectly! The problem now is in my server but that's other problem probably. Thanks you all!