restTemplate = new RestTemplate(getRequestFactory("", "", clientKeyStorePath, clientKeyStorePassword));
ResponseEntity<String> responseEntity = restTemplate.exchange(api, HttpMethod.GET, httpEntity, String.class);
HttpStatus httpStatus = responseEntity.getStatusCode();
String responseBody = responseEntity.getBody();
上面的代码如果请求返回的状态不是成功的。也就是返回的code不是200,那么将无法执行后续的代码,相反的会在exchange那一行抛出异常。也就是代码无法执行HttpStatus httpStatus = responseEntity.getStatusCode(); 以及后的代码。 如果我们想接收非200的状态,那么需要加上以下配置:
restTemplate.setErrorHandler(new NoErrorResultErrorHandler());
实际代码逻辑应该是:
restTemplate = new RestTemplate(getRequestFactory("", "", clientKeyStorePath, clientKeyStorePassword));
restTemplate.setErrorHandler(new NoErrorResultErrorHandler());
ResponseEntity<String> responseEntity = restTemplate.exchange(api, HttpMethod.GET, httpEntity, String.class);