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