當前位置: 首頁>>代碼示例>>Java>>正文


Java HttpClientErrorException.getResponseBodyAsString方法代碼示例

本文整理匯總了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());
	}
}
 
開發者ID:crnk-project,項目名稱:crnk-framework,代碼行數:24,代碼來源:BasicSpringBootTest.java

示例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");	
}
 
開發者ID:EsupPortail,項目名稱:esup-sgc,代碼行數:23,代碼來源:ApiCrousService.java

示例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());
	}
}
 
開發者ID:crnk-project,項目名稱:crnk-framework,代碼行數:13,代碼來源:RestTemplateRequest.java

示例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));
}
 
開發者ID:eclipse,項目名稱:keti,代碼行數:7,代碼來源:ACSTestUtil.java


注:本文中的org.springframework.web.client.HttpClientErrorException.getResponseBodyAsString方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。