本文整理匯總了Java中org.springframework.web.client.HttpClientErrorException.getResponseBodyAsString方法的典型用法代碼示例。如果您正苦於以下問題:Java HttpClientErrorException.getResponseBodyAsString方法的具體用法?Java HttpClientErrorException.getResponseBodyAsString怎麽用?Java HttpClientErrorException.getResponseBodyAsString使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.springframework.web.client.HttpClientErrorException
的用法示例。
在下文中一共展示了HttpClientErrorException.getResponseBodyAsString方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testErrorsSerializedAsJsonApi
import org.springframework.web.client.HttpClientErrorException; //導入方法依賴的package包/類
@Test
public void testErrorsSerializedAsJsonApi() throws IOException {
RestTemplate testRestTemplate = new RestTemplate();
try {
testRestTemplate
.getForEntity("http://localhost:" + this.port + "/doesNotExist", String.class);
Assert.fail();
}
catch (HttpClientErrorException e) {
assertEquals(HttpStatus.NOT_FOUND, e.getStatusCode());
String body = e.getResponseBodyAsString();
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(JacksonModule.createJacksonModule());
Document document = mapper.readerFor(Document.class).readValue(body);
Assert.assertEquals(1, document.getErrors().size());
ErrorData errorData = document.getErrors().get(0);
Assert.assertEquals("404", errorData.getStatus());
Assert.assertEquals("Not Found", errorData.getTitle());
Assert.assertEquals("No message available", errorData.getDetail());
}
}
示例2: postRightHolder
import org.springframework.web.client.HttpClientErrorException; //導入方法依賴的package包/類
private void postRightHolder(String eppn) {
String url = webUrl + "/rightholders";
HttpHeaders headers = this.getAuthHeaders();
RightHolder rightHolder = this.computeEsupSgcRightHolder(eppn);
HttpEntity entity = new HttpEntity(rightHolder, headers);
log.debug("Try to post to CROUS RightHolder : " + rightHolder);
try {
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
} catch(HttpClientErrorException clientEx) {
if(HttpStatus.LOCKED.equals(clientEx.getStatusCode())) {
if("Account is closed".equals(getErrorMessage(clientEx.getResponseBodyAsString()))) {
log.info("Account " + eppn + " is closed");
return;
} else {
throw new CrousAccountLockException(clientEx.getResponseBodyAsString(), clientEx);
}
} else {
throw clientEx;
}
}
log.info(eppn + " sent in CROUS as RightHolder");
}
示例3: execute
import org.springframework.web.client.HttpClientErrorException; //導入方法依賴的package包/類
@Override
public RestTemplateResponse execute() throws IOException {
HttpEntity<String> entityReq = new HttpEntity<>(requestBody, headers);
try {
ResponseEntity<String> response = template.exchange(url, HttpMethod.resolve(method.name()), entityReq, String.class);
return new RestTemplateResponse(response);
}
catch (HttpClientErrorException e) {
return new RestTemplateResponse(e.getRawStatusCode(), e.getStatusCode().getReasonPhrase(), e.getResponseBodyAsString
(), e.getResponseHeaders());
}
}
示例4: assertExceptionResponseBody
import org.springframework.web.client.HttpClientErrorException; //導入方法依賴的package包/類
public void assertExceptionResponseBody(final HttpClientErrorException e, final String message) {
String responseBody = e.getResponseBodyAsString();
Assert.assertNotNull(responseBody);
Assert.assertTrue(responseBody.contains(message),
String.format("Expected=[%s], Actual=[%s]", message, responseBody));
}