ResourceAccessException:POST请求上的I / O错误

时间:2019-12-18 19:38:10

标签: java spring spring-boot

尝试将数据发布到api时出现错误。下面是代码片段。希望很快能有所帮助!谢谢

String id= Comment.getId();
String authStr = "userName:password";
String base64Creds = Base64.getEncoder().encodeToString(authStr.getBytes());
// create headers
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Basic " + base64Creds);
headers.setContentType(MediaType.APPLICATION_JSON);
JSONObject commonJsonObject = new JSONObject();
commonJsonObject .put("body","password");
SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
// Connect timeout: time is in milliseconds
clientHttpRequestFactory.setConnectTimeout(3000);
// Read timeout: time is in milliseconds
clientHttpRequestFactory.setReadTimeout(3000);
HttpEntity<String> request = new HttpEntity<String>(commentJsonObject.toString(), headers);
RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory);
String newCommentString = restTemplate.postForObject(url, request, String.class);
  

7org.springframework.web.client.ResourceAccessException:“ jirasdtest.myoutotec.com/rest/api/2/issue/SD-1043/comment”的POST请求上的I / O错误:连接超时;嵌套的异常是java.net.SocketTimeoutException:连接在org.springframework.web.client.RestTemplate.execute(RestTemplate.java:670)的org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:744)处超时

1 个答案:

答案 0 :(得分:0)

选择SimpleClientHttpRequestFactory并不是最佳解决方案... 请尝试使用其他实现-HttpComponentsClientHttpRequestFactory

var requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setConnectTimeout(20000);
requestFactory.setConnectionRequestTimeout(20000);
requestFactory.setReadTimeout(30000);

SimpleClientHttpRequestFactory

  

使用标准JDK的ClientHttpRequestFactory实现   设施。

HttpComponentsClientHttpRequestFactory

  

使用Apache的ClientHttpRequestFactory实现   HttpComponents HttpClient创建请求。