如何在Spring REST服务中发出POST请求

时间:2019-10-08 07:41:06

标签: java spring rest

我需要休息一下,我想对URL进行POST。当我检查POSTMAN时,此网址有效。

对于POSTMAN,请求为

{

"username": "RenfeInbound",

"password": "renfeinbound"

}

这是我春季的休息时间

@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是我要发出请求的URL。

datosLogin是请求值

getHttpHeaders()方法是

 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;
        }

执行请求时,在控制台上出现以下错误

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)

2 个答案:

答案 0 :(得分:1)

Connection timed out有2种可能性

1-可能是您看不到自己的URL

所以首先尝试运行以下命令来检查与URL的连接

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

如果您遇到相同的例外情况,则必须检查邮递员上是否有代理人

2-远程服务器有一个问题,无法以您请求的数据响应您,因为您必须增加resttemplate超时参数,如下所示

//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;
}

您可以找到示例here

答案 1 :(得分:0)

“连接超时:”表示您的本地服务器无法连接到https://cloud-uat.transfesa.com/renfe-int-api/login

如果您将任何代理与邮递员一起使用,请在本地服务器中使用同一代理。或者相反,如果邮递员不使用任何代理,请从本地服务器中删除该代理。