下面列出了org.springframework.http.HttpMethod#name ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@DataProvider(value = {
"GET",
"HEAD",
"POST",
"PUT",
"PATCH",
"DELETE",
"OPTIONS",
"TRACE",
"null"
})
@Test
public void getRequestMethodAsString_works_as_expected(
HttpMethod method
) {
// given
String expectedResult = (method == null) ? "UNKNOWN_HTTP_METHOD" : method.name();
// when
String result = WingtipsSpringUtil.getRequestMethodAsString(method);
// then
assertThat(result).isEqualTo(expectedResult);
}
/**
* Execute the given method on the provided URI. The
* {@link org.springframework.http.client.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})
* @param responseExtractor object that extracts the return value from the response (can
* be {@code null})
* @return an arbitrary object, as returned by the {@link ResponseExtractor}
*/
protected <T> ListenableFuture<T> doExecute(URI url, HttpMethod method,
@Nullable AsyncRequestCallback requestCallback,
@Nullable ResponseExtractor<T> responseExtractor) throws RestClientException {
Assert.notNull(url, "'url' must not be null");
Assert.notNull(method, "'method' must not be null");
try {
org.springframework.http.client.AsyncClientHttpRequest request = createAsyncRequest(url, method);
if (requestCallback != null) {
requestCallback.doWithRequest(request);
}
ListenableFuture<ClientHttpResponse> responseFuture = request.executeAsync();
return new ResponseExtractorFuture<>(method, url, responseFuture, responseExtractor);
}
catch (IOException ex) {
throw new ResourceAccessException("I/O error on " + method.name() +
" request for \"" + url + "\":" + ex.getMessage(), ex);
}
}
/**
* Execute the given method on the provided URI. The
* {@link org.springframework.http.client.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})
* @param responseExtractor object that extracts the return value from the response (can
* be {@code null})
* @return an arbitrary object, as returned by the {@link ResponseExtractor}
*/
protected <T> ListenableFuture<T> doExecute(URI url, HttpMethod method,
@Nullable AsyncRequestCallback requestCallback,
@Nullable ResponseExtractor<T> responseExtractor) throws RestClientException {
Assert.notNull(url, "'url' must not be null");
Assert.notNull(method, "'method' must not be null");
try {
org.springframework.http.client.AsyncClientHttpRequest request = createAsyncRequest(url, method);
if (requestCallback != null) {
requestCallback.doWithRequest(request);
}
ListenableFuture<ClientHttpResponse> responseFuture = request.executeAsync();
return new ResponseExtractorFuture<>(method, url, responseFuture, responseExtractor);
}
catch (IOException ex) {
throw new ResourceAccessException("I/O error on " + method.name() +
" request for \"" + url + "\":" + ex.getMessage(), ex);
}
}
/**
* @return The value of {@link HttpMethod#name()}, or "UNKNOWN_HTTP_METHOD" if the given method is null.
*/
protected @NotNull String getRequestMethodAsString(@Nullable HttpMethod method) {
if (method == null) {
return "UNKNOWN_HTTP_METHOD";
}
return method.name();
}
/**
* Execute the given method on the provided URI.
* <p>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})
* @param responseExtractor object that extracts the return value from the response (can be {@code null})
* @return an arbitrary object, as returned by the {@link ResponseExtractor}
*/
@Nullable
protected <T> T doExecute(URI url, @Nullable HttpMethod method, @Nullable RequestCallback requestCallback,
@Nullable ResponseExtractor<T> responseExtractor) throws RestClientException {
Assert.notNull(url, "URI is required");
Assert.notNull(method, "HttpMethod is required");
ClientHttpResponse response = null;
try {
ClientHttpRequest request = createRequest(url, method);
if (requestCallback != null) {
requestCallback.doWithRequest(request);
}
response = request.execute();
handleResponse(url, method, response);
return (responseExtractor != null ? responseExtractor.extractData(response) : null);
}
catch (IOException ex) {
String resource = url.toString();
String query = url.getRawQuery();
resource = (query != null ? resource.substring(0, resource.indexOf('?')) : resource);
throw new ResourceAccessException("I/O error on " + method.name() +
" request for \"" + resource + "\": " + ex.getMessage(), ex);
}
finally {
if (response != null) {
response.close();
}
}
}
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
long begin = System.currentTimeMillis();
String httpMethodName = UNKNOWN_HTTP_METHOD;
HttpMethod method = request.getMethod();
if (method != null) {
httpMethodName = method.name();
}
String requestBody = IOUtils.toString(body, "utf-8");
try {
ClientHttpResponse response = execution.execute(request, body);
long cost = System.currentTimeMillis() - begin;
MediaType contentType = response.getHeaders().getContentType();
String responseContent;
if (contentType != null) {
String subtype = contentType.getSubtype();
if (subtype.contains("json") || contentType.getType().contains("text") || subtype.contains("xml")) {
responseContent = IOUtils.toString(response.getBody(), "utf-8");
} else {
responseContent = "neither text,nor xml,nor json";
}
} else {
responseContent = HAS_NO_CONTENT_TYPE;
}
if (cost > 3000) {
log.info("【特殊标识】慢接口 【 TODOLIST 】 大于3秒 cost:[{}]ms,{} {}. request:{},response:{},", cost, httpMethodName, request.getURI().toString(), requestBody, responseContent);
} else if (cost > 2000) {
log.info("【特殊标识】慢接口 【 TODOLIST 】 大于2秒 cost:[{}]ms,{} {}. request:{},response:{},", cost, httpMethodName, request.getURI().toString(), requestBody, responseContent);
} else {
log.info("cost:[{}]ms,on {} {}. request:{},response:{},", cost, httpMethodName, request.getURI().toString(), requestBody, responseContent);
}
return response;
} catch (IOException e) {
log.error("【特殊标识】【 TODOLIST 】 接口 cost:[{}]ms,I/O error on {} {}. request:{},response:{},", (System.currentTimeMillis() - begin), httpMethodName, request.getURI().toString(), requestBody, e.getMessage());
throw e;
}
}
@Override
public @Nullable String getRequestHttpMethod(@Nullable HttpRequest request) {
if (request == null) {
return null;
}
HttpMethod method = request.getMethod();
return (method == null) ? "UNKNOWN_HTTP_METHOD" : method.name();
}
/**
* @param method The HTTP method.
* @return "UNKNOWN_HTTP_METHOD" if the method is null, otherwise {@link HttpMethod#name()}.
*/
public static String getRequestMethodAsString(HttpMethod method) {
if (method == null) {
return "UNKNOWN_HTTP_METHOD";
}
return method.name();
}
/**
* Execute the given method on the provided URI.
* <p>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})
* @param responseExtractor object that extracts the return value from the response (can be {@code null})
* @return an arbitrary object, as returned by the {@link ResponseExtractor}
*/
protected <T> T doExecute(URI url, HttpMethod method, RequestCallback requestCallback,
ResponseExtractor<T> responseExtractor) throws RestClientException {
Assert.notNull(url, "'url' must not be null");
Assert.notNull(method, "'method' must not be null");
ClientHttpResponse response = null;
try {
ClientHttpRequest request = createRequest(url, method);
if (requestCallback != null) {
requestCallback.doWithRequest(request);
}
response = request.execute();
handleResponse(url, method, response);
if (responseExtractor != null) {
return responseExtractor.extractData(response);
}
else {
return null;
}
}
catch (IOException ex) {
throw new ResourceAccessException("I/O error on " + method.name() +
" request for \"" + url + "\": " + ex.getMessage(), ex);
}
finally {
if (response != null) {
response.close();
}
}
}
private String getOperationId(String nameFromAnnotation, Method method, HttpMethod httpMethod) {
return StringUtils.isBlank(nameFromAnnotation) ? method.getName() + "Using" + httpMethod.name() : nameFromAnnotation;
}
private String getOperationId(String nameFromAnnotation, Method method, HttpMethod httpMethod) {
return StringUtils.isBlank(nameFromAnnotation) ? method.getName() + "Using" + httpMethod.name() : nameFromAnnotation;
}
public MethodNotAllowedException(HttpMethod method, Collection<HttpMethod> supportedMethods) {
this(method.name(), supportedMethods);
}
@Override
public ServerHttpRequest.Builder method(HttpMethod httpMethod) {
this.httpMethodValue = httpMethod.name();
return this;
}
@Override
public ServerHttpRequest.Builder method(HttpMethod httpMethod) {
this.httpMethodValue = httpMethod.name();
return this;
}
private String getWebscriptName(String webscript, HttpMethod httpMethod) {
String beanName = "webscript." + webscript + "." + httpMethod.name();
return beanName.toLowerCase();
}
private <T> ResponseEntity<T> sendRequest(URI uri, HttpMethod method, HttpEntity httpEntity, Class<T> responseType) {
if (isFailSafeEnabled) {
String url = method.name() + ":" + uri.toString();
return circuitBreakerManager.failsafe(
() -> this.restTemplate.exchange(uri, method, httpEntity, responseType),
url
);
}
return this.restTemplate.exchange(uri, method, httpEntity, responseType);
}
/**
* Package private constructor. To get an instance, use static factory
* methods in {@link MockMvcRequestBuilders}.
* <p>Although this class cannot be extended, additional ways to initialize
* the {@code MockHttpServletRequest} can be plugged in via
* {@link #with(RequestPostProcessor)}.
* @param httpMethod the HTTP method (GET, POST, etc)
* @param url a URL template; the resulting URL will be encoded
* @param vars zero or more URI variables
*/
MockHttpServletRequestBuilder(HttpMethod httpMethod, String url, Object... vars) {
this(httpMethod.name(), UriComponentsBuilder.fromUriString(url).buildAndExpand(vars).encode().toUri());
}
/**
* Alternative to {@link #MockHttpServletRequestBuilder(HttpMethod, String, Object...)}
* with a pre-built URI.
* @param httpMethod the HTTP method (GET, POST, etc)
* @param url the URL
* @since 4.0.3
*/
MockHttpServletRequestBuilder(HttpMethod httpMethod, URI url) {
this(httpMethod.name(), url);
}
/**
* Package private constructor. To get an instance, use static factory
* methods in {@link MockMvcRequestBuilders}.
* <p>Although this class cannot be extended, additional ways to initialize
* the {@code MockHttpServletRequest} can be plugged in via
* {@link #with(RequestPostProcessor)}.
* @param httpMethod the HTTP method (GET, POST, etc)
* @param url a URL template; the resulting URL will be encoded
* @param vars zero or more URI variables
*/
MockHttpServletRequestBuilder(HttpMethod httpMethod, String url, Object... vars) {
this(httpMethod.name(), UriComponentsBuilder.fromUriString(url).buildAndExpand(vars).encode().toUri());
}
/**
* Alternative to {@link #MockHttpServletRequestBuilder(HttpMethod, String, Object...)}
* with a pre-built URI.
* @param httpMethod the HTTP method (GET, POST, etc)
* @param url the URL
* @since 4.0.3
*/
MockHttpServletRequestBuilder(HttpMethod httpMethod, URI url) {
this(httpMethod.name(), url);
}