0

I have a service rest and I want to do a POST to a URL. When I check with POSTMAN this url works.

With POSTMAN, the request is

{

"username": "RenfeInbound",

"password": "renfeinbound"

}

This is my spring service rest

@Override
    public RespuestaTransfesaBean peticionLogin(PeticionLoginTransfesa datosLogin) {

        RespuestaTransfesaBean res = null;
        try {

            RestTemplate restTemplate = new RestTemplate();

            HttpEntity<PeticionLoginTransfesa> request = new HttpEntity<>(datosLogin, getHttpHeaders());
            res = restTemplate.postForObject(TRANSFESA_URL, request, RespuestaTransfesaBean.class);

TRANSFESA_URL is the URL where I want to do the request.

datosLogin are the request values

Method getHttpHeaders() is

 private HttpHeaders getHttpHeaders() {
            if (headers == null) {
                headers = new HttpHeaders();
               // headers.add("username", TRANSFESA_USER);
               // headers.add("password", TRANSFESA_PASS);
                headers.add("Content-Type", "application/json");
            }

            return headers;
        }

When I execute the request, I get the following error on the console

org.springframework.web.client.ResourceAccessException: I/O error on POST request for "https://cloud-uat.transfesa.com/renfe-int-api/login": Connection timed out: connect; nested exception is java.net.ConnectException: Connection timed out: connect
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:743)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:669)
    at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:413)
    at com.renfe.bcm.externas.transfesa.service.TransfesaServiceImpl.peticionLogin(TransfesaServiceImpl.java:35)
    at com.renfe.bcm.externas.transfesa.restcontroller.TransfesaRestController.loginTransfesa(TransfesaRestController.java:23)
    at com.renfe.bcm.externas.transfesa.restcontroller.TransfesaRestController$$FastClassBySpringCGLIB$$9aa75999.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
M. Twarog
  • 2,418
  • 3
  • 21
  • 39
Carlota
  • 1,239
  • 3
  • 29
  • 59

2 Answers2

0

'Connection timed out:' indicates your local server is not able to connect to https://cloud-uat.transfesa.com/renfe-int-api/login

If you are using any proxy with postman, use the same proxy in your local server. Or inversely, if postman is not using any proxy, remove the proxy from local server.

Smile
  • 3,832
  • 3
  • 25
  • 39
0

Connection timed out there is 2 possibilities

1- may be you can't see your URL

so firstly try to run the following commend to check connectivity with URL

curl --header "Content-Type: application/json" \
  --request POST \
  --data '{"username":"xyz","password":"xyz"}' \
  https://cloud-uat.transfesa.com/renfe-int-api/login

if the you get the same exception, you have to check if there is a proxy on you postman or not

2- there is a problem with your remote server is slow to respond on you with your requested data for that you have to increase resttemplate timeout param like the following

//Create resttemplate
RestTemplate restTemplate = new RestTemplate(getClientHttpRequestFactory());

//Override timeouts in request factory
private SimpleClientHttpRequestFactory getClientHttpRequestFactory()
{
    SimpleClientHttpRequestFactory clientHttpRequestFactory
                      = new SimpleClientHttpRequestFactory();
    //Connect timeout
    clientHttpRequestFactory.setConnectTimeout(10_000);

    //Read timeout
    clientHttpRequestFactory.setReadTimeout(10_000);
    return clientHttpRequestFactory;
}

you can find example here

fathy elshemy
  • 481
  • 5
  • 18