springboot webflux webclient 使用说明
应用:webflux可使用webclient获取网络资源
************************
相关类与接口
WebClient
public interface WebClient {
WebClient.RequestHeadersUriSpec<?> get();
WebClient.RequestHeadersUriSpec<?> head();
WebClient.RequestBodyUriSpec post();
WebClient.RequestBodyUriSpec put();
WebClient.RequestBodyUriSpec patch();
WebClient.RequestHeadersUriSpec<?> delete();
WebClient.RequestHeadersUriSpec<?> options();
WebClient.RequestBodyUriSpec method(HttpMethod var1);
WebClient.Builder mutate();
static WebClient create() { //创建WebClient对象
return (new DefaultWebClientBuilder()).build();
}
static WebClient create(String baseUrl) {
return (new DefaultWebClientBuilder()).baseUrl(baseUrl).build();
}
static WebClient.Builder builder() {
return new DefaultWebClientBuilder();
}
public interface RequestBodyUriSpec extends WebClient.RequestBodySpec, WebClient.RequestHeadersUriSpec<WebClient.RequestBodySpec> {
}
public interface RequestHeadersUriSpec<S extends WebClient.RequestHeadersSpec<S>> extends WebClient.UriSpec<S>, WebClient.RequestHeadersSpec<S> {
}
public interface ResponseSpec {
WebClient.ResponseSpec onStatus(Predicate<HttpStatus> var1, Function<ClientResponse, Mono<? extends Throwable>> var2);
WebClient.ResponseSpec onRawStatus(IntPredicate var1, Function<ClientResponse, Mono<? extends Throwable>> var2);
<T> Mono<T> bodyToMono(Class<T> var1);
<T> Mono<T> bodyToMono(ParameterizedTypeReference<T> var1);
//使用该方法可将返回结果转换为指定的类型,
//如:new ParameterizedTypeReference<Map<String,String>> 转换为 Map<String,String>
// new ParameterizedTypeReference<Person> 转换为 Person类
<T> Flux<T> bodyToFlux(Class<T> var1);
<T> Flux<T> bodyToFlux(ParameterizedTypeReference<T> var1);
<T> Mono<ResponseEntity<T>> toEntity(Class<T> var1);
<T> Mono<ResponseEntity<T>> toEntity(ParameterizedTypeReference<T> var1);
<T> Mono<ResponseEntity<List<T>>> toEntityList(Class<T> var1);
<T> Mono<ResponseEntity<List<T>>> toEntityList(ParameterizedTypeReference<T> var1);
Mono<ResponseEntity<Void>> toBodilessEntity();
}
public interface RequestBodySpec extends WebClient.RequestHeadersSpec<WebClient.RequestBodySpec> {
WebClient.RequestBodySpec contentLength(long var1);
WebClient.RequestBodySpec contentType(MediaType var1);
WebClient.RequestHeadersSpec<?> bodyValue(Object var1);
<T, P extends Publisher<T>> WebClient.RequestHeadersSpec<?> body(P var1, Class<T> var2);
<T, P extends Publisher<T>> WebClient.RequestHeadersSpec<?> body(P var1, ParameterizedTypeReference<T> var2);
WebClient.RequestHeadersSpec<?> body(Object var1, Class<?> var2);
WebClient.RequestHeadersSpec<?> body(Object var1, ParameterizedTypeReference<?> var2);
WebClient.RequestHeadersSpec<?> body(BodyInserter<?, ? super ClientHttpRequest> var1);
/** @deprecated */
@Deprecated
WebClient.RequestHeadersSpec<?> syncBody(Object var1);
}
public interface RequestHeadersSpec<S extends WebClient.RequestHeadersSpec<S>> {
S accept(MediaType... var1);
S acceptCharset(Charset... var1);
S cookie(String var1, String var2);
S cookies(Consumer<MultiValueMap<String, String>> var1);
S ifModifiedSince(ZonedDateTime var1);
S ifNoneMatch(String... var1);
S header(String var1, String... var2);
S headers(Consumer<HttpHeaders> var1);
S attribute(String var1, Object var2);
S attributes(Consumer<Map<String, Object>> var1);
WebClient.ResponseSpec retrieve();
Mono<ClientResponse> exchange();
}
public interface UriSpec<S extends WebClient.RequestHeadersSpec<?>> {
S uri(URI var1); //设置请求的uri
S uri(String var1, Object... var2);
S uri(String var1, Map<String, ?> var2);
S uri(String var1, Function<UriBuilder, URI> var2);
S uri(Function<UriBuilder, URI> var1);
}
public interface Builder {
WebClient.Builder baseUrl(String var1);
WebClient.Builder defaultUriVariables(Map<String, ?> var1);
WebClient.Builder uriBuilderFactory(UriBuilderFactory var1);
WebClient.Builder defaultHeader(String var1, String... var2);
WebClient.Builder defaultHeaders(Consumer<HttpHeaders> var1);
WebClient.Builder defaultCookie(String var1, String... var2);
WebClient.Builder defaultCookies(Consumer<MultiValueMap<String, String>> var1);
WebClient.Builder defaultRequest(Consumer<WebClient.RequestHeadersSpec<?>> var1);
WebClient.Builder filter(ExchangeFilterFunction var1);
WebClient.Builder filters(Consumer<List<ExchangeFilterFunction>> var1);
WebClient.Builder clientConnector(ClientHttpConnector var1);
WebClient.Builder codecs(Consumer<ClientCodecConfigurer> var1);
WebClient.Builder exchangeStrategies(ExchangeStrategies var1);
/** @deprecated */
@Deprecated
WebClient.Builder exchangeStrategies(Consumer<org.springframework.web.reactive.function.client.ExchangeStrategies.Builder> var1);
WebClient.Builder exchangeFunction(ExchangeFunction var1);
WebClient.Builder apply(Consumer<WebClient.Builder> var1);
WebClient.Builder clone();
WebClient build();
}
}
DefaultWebClient:默认实现类
class DefaultWebClient implements WebClient {
private static final String URI_TEMPLATE_ATTRIBUTE = WebClient.class.getName() + ".uriTemplate";
private static final Mono<ClientResponse> NO_HTTP_CLIENT_RESPONSE_ERROR = Mono.error(new IllegalStateException("The underlying HTTP client completed without emitting a response."));
private final ExchangeFunction exchangeFunction;
private final UriBuilderFactory uriBuilderFactory;
@Nullable
private final HttpHeaders defaultHeaders;
@Nullable
private final MultiValueMap<String, String> defaultCookies;
@Nullable
private final Consumer<RequestHeadersSpec<?>> defaultRequest;
private final DefaultWebClientBuilder builder;
***********
构造方法
DefaultWebClient(ExchangeFunction exchangeFunction, @Nullable UriBuilderFactory factory, @Nullable HttpHeaders defaultHeaders, @Nullable MultiValueMap<String, String> defaultCookies, @Nullable Consumer<RequestHeadersSpec<?>> defaultRequest, DefaultWebClientBuilder builder) {
this.exchangeFunction = exchangeFunction;
this.uriBuilderFactory = (UriBuilderFactory)(factory != null ? factory : new DefaultUriBuilderFactory());
this.defaultHeaders = defaultHeaders;
this.defaultCookies = defaultCookies;
this.defaultRequest = defaultRequest;
this.builder = builder;
}
***********
普通方法
public RequestHeadersUriSpec<?> get() {
return this.methodInte