本文整理汇总了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));
}