Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在springrest服务中进行POST请求_Java_Spring_Rest - Fatal编程技术网

Java 如何在springrest服务中进行POST请求

Java 如何在springrest服务中进行POST请求,java,spring,rest,Java,Spring,Rest,我有一个服务休息,我想做一个帖子到一个网址。当我与邮递员核对时,这个url是有效的 对于邮递员,请求是 { } 这是我的春季服务休息 @Override public RespuestaTransfesaBean peticionLogin(PeticionLoginTransfesa datosLogin) { RespuestaTransfesaBean res = null; try { RestTemplate rest

我有一个服务休息,我想做一个帖子到一个网址。当我与邮递员核对时,这个url是有效的

对于邮递员,请求是

{

}

这是我的春季服务休息

@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);
当我执行请求时,我在控制台上得到以下错误

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)
org.springframework.web.client.ResourceAccessException:POST请求的I/O错误“https://cloud-uat.transfesa.com/renfe-int-api/login“:连接超时:连接;嵌套异常为java.net.ConnectException:连接超时:连接
位于org.springframework.web.client.restemplate.doExecute(restemplate.java:743)
位于org.springframework.web.client.restemplate.execute(restemplate.java:669)
位于org.springframework.web.client.restemplate.postForObject(restemplate.java:413)
位于com.renfe.bcm.externas.transfesa.service.transfesaserviceinpl.peticionLogin(transfesaserviceinpl.java:35)
位于com.renfe.bcm.externas.transfesa.restcontroller.TransfesaRestController.loginTransfesa(TransfesaRestController.java:23)
位于com.renfe.bcm.externas.transfesa.restcontroller.TransfesaRestController$$FastClassBySpringCGLIB$$9aa75999.invoke()
位于org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)

“连接超时:”表示您的本地服务器无法连接到


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

连接超时
有两种可能性

1-可能是你看不到你的

因此,首先尝试运行以下命令来检查与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 timeout参数,如下所示

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

你可以找到我和邮递员试过的例子,它不起作用,我检查了一下,它起作用了
curl --header "Content-Type: application/json" \
  --request POST \
  --data '{"username":"xyz","password":"xyz"}' \
  https://cloud-uat.transfesa.com/renfe-int-api/login
//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;
}