Java Code Examples for org.springframework.web.client.HttpStatusCodeException#getResponseBodyAsString()

The following examples show how to use org.springframework.web.client.HttpStatusCodeException#getResponseBodyAsString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractServiceApiPlusListener.java    From MicroCommunity with Apache License 2.0 6 votes vote down vote up
/**
 * 调用下游服务
 *
 * @param context
 * @return
 */
public ResponseEntity<String> callOrderService(DataFlowContext context, JSONObject paramIn) {

    context.getRequestCurrentHeaders().put(CommonConstant.HTTP_ORDER_TYPE_CD, "D");
    ResponseEntity responseEntity = null;
    if (paramIn == null || paramIn.isEmpty()) {
        paramIn = context.getReqJson();
    }
    String serviceUrl = ORDER_SERVICE_URL;
    HttpEntity<String> httpEntity = null;
    HttpHeaders header = new HttpHeaders();
    for (String key : context.getRequestCurrentHeaders().keySet()) {
        if (CommonConstant.HTTP_SERVICE.toLowerCase().equals(key.toLowerCase())) {
            continue;
        }
        header.add(key, context.getRequestCurrentHeaders().get(key));
    }
    try {
        httpEntity = new HttpEntity<String>(JSONObject.toJSONString(paramIn), header);
        responseEntity = restTemplate.exchange(serviceUrl, HttpMethod.POST, httpEntity, String.class);
    } catch (HttpStatusCodeException e) { //这里spring 框架 在4XX 或 5XX 时抛出 HttpServerErrorException 异常,需要重新封装一下
        responseEntity = new ResponseEntity<String>(e.getResponseBodyAsString(), e.getStatusCode());
    }
    return responseEntity;
}
 
Example 2
Source File: StandardBullhornData.java    From sdk-rest with MIT License 6 votes vote down vote up
/**
 * @param uriVariables
 * @param tryNumber
 * @param error
 * @throws RestApiException if tryNumber >= API_RETRY.
 */
protected boolean handleHttpStatusCodeError(Map<String, String> uriVariables, int tryNumber, HttpStatusCodeException error) {
    boolean isTooManyRequestsError = false;
    if (error.getStatusCode() == HttpStatus.UNAUTHORIZED) {
        resetBhRestToken(uriVariables);
    } else if (error.getStatusCode() == HttpStatus.TOO_MANY_REQUESTS) {
        isTooManyRequestsError = true;
    }
    log.error(
            "HttpStatusCodeError making api call. Try number:" + tryNumber + " out of " + API_RETRY + ". Http status code: "
                    + error.getStatusCode() + ". Response body: " + error.getResponseBodyAsString(), error);
    if (tryNumber >= API_RETRY && !isTooManyRequestsError) {
        throw new RestApiException("HttpStatusCodeError making api call with url variables " + uriVariables.toString()
                + ". Http status code: " + error.getStatusCode().toString() + ". Response body: " + error == null ? ""
                : error.getResponseBodyAsString());
    }
    return isTooManyRequestsError;
}
 
Example 3
Source File: EchoResponseErrorHandler.java    From Milkomeda with MIT License 5 votes vote down vote up
@Override
public void handleError(ClientHttpResponse response) throws IOException {
    try {
        errorHandler.handleError(response);
    } catch (RestClientException e) {
        if (e instanceof HttpStatusCodeException) {
            HttpStatusCodeException ce = (HttpStatusCodeException) e;
            log.error("Echo request with error code: {}, msg:{}, body:{}", ce.getStatusCode().value(), ce.getMessage(), ce.getResponseBodyAsString());
            throw new EchoException(ce.getStatusCode().value(), ce.getMessage(), ce.getResponseBodyAsString());
        }
        throw new EchoException(ErrorCode.VENDOR_REQUEST_ERROR, e.getMessage());
    }
}
 
Example 4
Source File: ClientHttpRequestFactoryFactoryIntegrationTests.java    From spring-vault with Apache License 2.0 5 votes vote down vote up
private String request(RestTemplate template) {

		// Uninitialized and sealed can cause status 500
		try {
			ResponseEntity<String> responseEntity = template.exchange(this.url, HttpMethod.GET, null, String.class);
			return responseEntity.getBody();
		}
		catch (HttpStatusCodeException e) {
			return e.getResponseBodyAsString();
		}
	}
 
Example 5
Source File: RestTemplateHttpClient.java    From trello-java-wrapper with Apache License 2.0 5 votes vote down vote up
private static RuntimeException translateException(HttpStatusCodeException e) {
    String response = e.getResponseBodyAsString();

    switch (e.getStatusCode()) {
        case BAD_REQUEST:
            return new TrelloBadRequestException(response, e);
        case NOT_FOUND:
            return new NotFoundException(response, e);
        case UNAUTHORIZED:
            return new NotAuthorizedException(response, e);
        default:
            return new TrelloHttpException(e);
    }
}