当前位置: 首页>>代码示例>>Java>>正文


Java RequestCallback.doWithRequest方法代码示例

本文整理汇总了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());
	}
}
 
开发者ID:bestarandyan,项目名称:ShoppingMall,代码行数:64,代码来源:CustomRestTemplate.java


注:本文中的org.springframework.web.client.RequestCallback.doWithRequest方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。