码迷,mamicode.com
首页 > Web开发 > 详细

WebFlux系列(十一)WebClient 日志

时间:2020-01-18 21:01:48      阅读:89      评论:0      收藏:0      [点我收藏+]

标签:RoCE   imp   坚持   cal   log   ima   org   ati   ppi   

#Java#Spring#WebClient#WebFlux#log#日志#

WebClient 日志

视频讲解 : https://www.bilibili.com/video/av83627944/

 

技术图片

WebfluxConsumerApplication.java
package com.example.webfluxconsumer;

import lombok.extern.log4j.Log4j2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

@Log4j2
@SpringBootApplication
public class WebfluxConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(WebfluxConsumerApplication.class, args);
    }
@RestController
class EmployeeController {
    @PostMapping("save")
    public Mono<Boolean> save(@RequestBody Mono<Employee> employeeMono) {
        WebClient webClient  = WebClient.builder().baseUrl("http://localhost:8080/save")
                .filter(logRequest())
                .filter(logResponse())
                .build();
        return webClient.post().body(employeeMono, Employee.class).retrieve().bodyToMono(Boolean.class);
    }
    private ExchangeFilterFunction logRequest() {
        return (clientRequest, next) -> {
            log.info("Request: {} {}", clientRequest.method(), clientRequest.url());
            clientRequest.headers()
                    .forEach((name, values) -> values.forEach(value -> log.info("Request: {}={}", name, value)));
            return next.exchange(clientRequest);
        };
    }
    private ExchangeFilterFunction logResponse() {
        return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> {
            clientResponse.headers().asHttpHeaders()
                    .forEach((name, values) -> values.forEach(value -> log.info("Response: {}={}", name, value)));
            return Mono.just(clientResponse);
        });
    }
}
}

公众号,坚持每天3分钟视频学习

技术图片

WebFlux系列(十一)WebClient 日志

标签:RoCE   imp   坚持   cal   log   ima   org   ati   ppi   

原文地址:https://www.cnblogs.com/JavaWeiBianCheng/p/12209967.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!