public void postTest() {
String url = "https://xxxxxxxx";
RestTemplate restTemplate = new RestTemplate();
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setOutputStreaming(false);
restTemplate.setRequestFactory(requestFactory);
restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
@Override
protected boolean hasError(HttpStatus statusCode) {
return true;
}
@Override
public void handleError(ClientHttpResponse response) {
}
});
HttpHeaders headers = new HttpHeaders();
HttpEntity<String> entity = new HttpEntity<>("JSON参数", headers);
ResponseEntity<String> result = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
int code = result.getStatusCodeValue();
System.out.println(code);
System.out.println(result.getBody());
}
RestTemplate请求处理时,返回HTTP错误状态码处理记录
最新推荐文章于 2024-10-11 10:30:33 发布