WebClient
2019-07-18 17:03:21 6 举报
AI智能生成
Spring5 WebClient总结
作者其他创作
大纲/内容
创建&配置
创建
create()
WebClient webClient = WebClient.create()
WebClient webClient =
WebClient.create("https://localhost:8081/v1/accounts");
WebClient.create("https://localhost:8081/v1/accounts");
builder()
WebClient webClient = WebClient.builder().build();
配置
WebClient webClient =
WebClient.builder().baseUrl("https://localhost:8081/v1/accounts")
.defaultHeader(HttpHeaders.CONTENT_TYPE, "application/json")
.defaultHeader(HttpHeaders.USER_AGENT, "Reactive WebClient")
.build();
WebClient.builder().baseUrl("https://localhost:8081/v1/accounts")
.defaultHeader(HttpHeaders.CONTENT_TYPE, "application/json")
.defaultHeader(HttpHeaders.USER_AGENT, "Reactive WebClient")
.build();
请求体
body()
Mono<Account> accountMono = ...;
Mono<Void> result = webClient.post()
.uri("http://localhost:8081/v1/account/{id}", id)
.contentType(MediaType.APPLICATION_JSON)
.body(accountMono, Account.class)
.retrieve()
.bodyToMono(Void.class);
Mono<Void> result = webClient.post()
.uri("http://localhost:8081/v1/account/{id}", id)
.contentType(MediaType.APPLICATION_JSON)
.body(accountMono, Account.class)
.retrieve()
.bodyToMono(Void.class);
syncBody()
Mono<Void> result = webClient.post()
.uri("http://localhost:8081/v1/account/{id}", id)
.contentType(MediaType.APPLICATION_JSON)
.syncBody(account)
.retrieve()
.bodyToMono(Void.class);
.uri("http://localhost:8081/v1/account/{id}", id)
.contentType(MediaType.APPLICATION_JSON)
.syncBody(account)
.retrieve()
.bodyToMono(Void.class);
Form&Multipart
Form
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("username", "zhangsan");
map.add("password", "1234.abcd");
Mono<String> mono = webClient.post()
.uri("http://localhost:8081/login")
.synBody(map)
.retrieve()
.bodyToMono(String.class);
map.add("username", "zhangsan");
map.add("password", "1234.abcd");
Mono<String> mono = webClient.post()
.uri("http://localhost:8081/login")
.synBody(map)
.retrieve()
.bodyToMono(String.class);
MultipartBodyBuilder
MultipartBodyBuilder builder = new MultipartBodyBuilder();
builder.part("fieldPart", "fieldValue");
builder.part("filePart", new FileSystemResource("logo.png"));
builder.part("jsonPart", new Account("zhangsan"));
MultiValueMap<String, HttpEntity<?>> parts = builder.build();
builder.part("fieldPart", "fieldValue");
builder.part("filePart", new FileSystemResource("logo.png"));
builder.part("jsonPart", new Account("zhangsan"));
MultiValueMap<String, HttpEntity<?>> parts = builder.build();
错误处理
retrieve()
Flux<Account> flux = webClient.get()
.uri("http://localhost:8081/v1/account?sort={sort}&direction={direction}", "updated", "desc")
.retrieve()
.onStatus(HttpStatus::is4xxClientError, clientResponse -> Mono.error(new MyCustomClientException()))
.onStatus(HttpStatus::is5xxServerError, clientResponse -> Mono.error(new MyCustomClientException()))
.bodyToFlux(Account.class);
.uri("http://localhost:8081/v1/account?sort={sort}&direction={direction}", "updated", "desc")
.retrieve()
.onStatus(HttpStatus::is4xxClientError, clientResponse -> Mono.error(new MyCustomClientException()))
.onStatus(HttpStatus::is5xxServerError, clientResponse -> Mono.error(new MyCustomClientException()))
.bodyToFlux(Account.class);
exchange()
自己处理
检查状态码
构造URL
动态参数
webClient.get().uri("http://localhost:8081/account/{p1}/{p2}", "var1", "var2");
Map
Map<String, Object> uriVariables = new HashMap<>();
uriVariables.put("p1", "var1");
uriVariables.put("p2", 1);
webClient.get().uri("http://localhost:8081/account/{p1}/{p2}", uriVariables);
uriVariables.put("p1", "var1");
uriVariables.put("p2", 1);
webClient.get().uri("http://localhost:8081/account/{p1}/{p2}", uriVariables);
uriBuilder
webClient.get()
.uri(uriBuilder -> uriBuilder.path("/user/orders")
.queryParam("sort", "updated").queryParam("direction", "desc").build())
.uri(uriBuilder -> uriBuilder.path("/user/orders")
.queryParam("sort", "updated").queryParam("direction", "desc").build())
响应体
retrieve()
JSON序列化
Mono<Account> result = webClient.get()
.uri("http://localhost:8081/v1/account/{id}", id)
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.bodyToMono(Account.class);
.uri("http://localhost:8081/v1/account/{id}", id)
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.bodyToMono(Account.class);
流
Flux<Account> result = webClient.get()
.uri("http://localhost:8081/v1/accounts")
.accept(MediaType.TEXT_EVENT_STREAM)
.retrieve()
.bodyToFlux(Account.class);
.uri("http://localhost:8081/v1/accounts")
.accept(MediaType.TEXT_EVENT_STREAM)
.retrieve()
.bodyToFlux(Account.class);
exchange()
Mono<Account> result = webClient.get()
.uri("http://localhost:8081/v1/account/{id}", id)
.accept(MediaType.APPLICATION_JSON)
.exchange()
.flatMap(response -> response.bodyToMono(Account.class));
.uri("http://localhost:8081/v1/account/{id}", id)
.accept(MediaType.APPLICATION_JSON)
.exchange()
.flatMap(response -> response.bodyToMono(Account.class));
过滤器
private ExchangeFilterFunction logRequest() {
return (clientRequest, next) -> {
logger.info("Request:{}{}", clientRequest.method(), clientRequest.url());
clientRequest.headers().forEach((name, values) -> {
values.forEach(value -> {
logger.info("{}={}", name, value);
});
});
return next.exchange(clientRequest);
};
}
WebClient webClient = WebClient.builder()
.filter(logRequest())
.build();
return (clientRequest, next) -> {
logger.info("Request:{}{}", clientRequest.method(), clientRequest.url());
clientRequest.headers().forEach((name, values) -> {
values.forEach(value -> {
logger.info("{}={}", name, value);
});
});
return next.exchange(clientRequest);
};
}
WebClient webClient = WebClient.builder()
.filter(logRequest())
.build();
0 条评论
下一页