本文整理汇总了Java中org.springframework.web.client.RequestCallback.doWithRequest方法的典型用法代码示例。如果您正苦于以下问题:Java RequestCallback.doWithRequest方法的具体用法?Java RequestCallback.doWithRequest怎么用?Java RequestCallback.doWithRequest使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.web.client.RequestCallback
的用法示例。
在下文中一共展示了RequestCallback.doWithRequest方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doExecute
import org.springframework.web.client.RequestCallback; //导入方法依赖的package包/类
/**
* Execute the given method on the provided URI. The {@link ClientHttpRequest} is processed using the {@link RequestCallback}; the
* response with the {@link ResponseExtractor}.
*
* @param url
* the fully-expanded URL to connect to
* @param method
* the HTTP method to execute (GET, POST, etc.)
* @param requestCallback
* object that prepares the request (can be <code>null</code>)
* @param responseExtractor
* object that extracts the return value from the response (can be <code>null</code>)
* @return an arbitrary object, as returned by the {@link ResponseExtractor}
*/
protected <T> T doExecute(URI url, HttpMethod method, RequestCallback requestCallback, ResponseExtractor<T> responseExtractor) {
Assert.notNull(url, "'url' must not be null");
Assert.notNull(method, "'method' must not be null");
ClientHttpResponse response = null;
ClientHttpRequest request = null;
try {
// add by leo
if (containsRequest(url.toString()))
abortRequest(url.toString());
request = createRequest(url, method);
if (requestCallback != null) {
requestCallback.doWithRequest(request);
}
response = request.execute();
if (!getErrorHandler().hasError(response)) {
logResponseStatus(method, url, response);
} else {
handleResponseError(method, url, response);
}
if (responseExtractor != null) {
return responseExtractor.extractData(response);
} else {
return null;
}
} catch (ConnectTimeoutException e) {
throw new ClientException("请求超时,请稍后重试!", ClientException.REQUEST_NETWORK, e);
} catch (Exception ex) {
if (ex.getMessage() != null) {
String str = ex.getMessage().toLowerCase(Locale.getDefault());
if (str.contains("shutdown") || str.contains("request already aborted") || str.contains("socket closed"))
throw new ClientException("request already aborted", ClientException.REQUEST_ABORTED);
}
throw new ServerException("请求异常,请稍后再试!", ex);
} finally {
if (response != null) {
response.close();
}
removeRequest(url.toString());
}
}