【问题标题】:Spring Webflux - aop - How to get request/response bodySpring Webflux - aop - 如何获取请求/响应正文
【发布时间】:2018-01-10 17:01:00
【问题描述】:

我正在使用 Spring-webflux 来创建 RouterFunction 和 Handler。我有 @Aspect for My handler 函数如下,将 ServerRequest 正文和 ServerResponse 正文存储在数据库中。但是当尝试获取 Object 时,我的请求被挂起。是否有任何示例代码来实现此功能。

@Around("@annotation(Log")
public Object log(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
    Object o = proceedingJoinPoint.proceed();
    ServerRequest serverRequest = ServerRequest.class.cast(proceedingJoinPoint.getArgs()[0]);
    Mono<Customer> customerMono = serverRequest.bodyToMono(Customer.class);
    Customer customer = customerMono.block() //Request Hanged here
    log.info("customer" + customer);
    return o;
}

【问题讨论】:

  • 您找到解决方案了吗?
  • 请在这个帖子stackoverflow.com/a/50712697/3073945查看我的答案
  • @Md.SajedulKarim 已经很久了......但是我有一个相关的问题,因为我正在尝试解决类似的问题。您提供的答案仅与发送显式请求(即通过 POST)的用例有关。如果我想获取整个请求(包括标头),有哪些选择? IE。来自 GET 的原始请求。所以不是请求正文,只有标题。那么......它不是参数的一部分(getArgs())......如何访问这种类型的请求?非常感谢。

标签: spring spring-boot spring-webflux


【解决方案1】:

block() 是一个阻塞调用,因此当你调用它时它会挂起。

要从Mono&lt;Customer&gt; customer 中打印出客户数据,请尝试以下操作:

customerMono.flatMap(customer -> System.out.println(customer)); // longhand form
customerMono.flatMap(System.out::println); // shorthand form of the above

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-23
    • 2018-10-07
    • 2021-01-28
    • 1970-01-01
    • 2019-10-02
    • 2020-08-25
    • 2017-12-27
    • 1970-01-01
    相关资源
    最近更新 更多