【问题标题】:Webflux-Aop:Get method request(Mono) in Aspect classWebflux-Aop:在 Aspect 类中获取方法请求(单声道)
【发布时间】:2021-07-29 03:35:04
【问题描述】:

我正在尝试在调用服务实现类之前编写一个负责调用一些外部系统的方面。

我很难在方面类中获取方法请求。任何帮助将不胜感激。我试着写如下,但没有运气在方面类中获得 Mono Object。

方法

@someAnnotation
@PutMapping
fun getAccounts(
    @RequestParam(name = "someParam")
    someParam: String?,
    @ModelAttribute
    request: Mono<Request>,
): Mono<Response>{
        return response;
    }

注释

@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class someAnnotation(
    val api: String,
)

方面:

@Aspect
@Component
@Order(0)
class SomeAspect{
    @Suppress("UNCHECKED_CAST")
    @Around("@annotation(someAnnotation)&&args(req,..)")
    @Throws(Throwable::class)
    fun <T> getResponse(joinPoint: ProceedingJoinPoint,apiReq : Mono<T>): Mono<T> {
        
        val target = joinPoint.target
        val methodSignature = joinPoint.signature as MethodSignature
        val method: Method = methodSignature.method

        val flux: Mono<T> = joinPoint.proceed() as Mono<T>
        val methodArgs = joinPoint.args

//How to get request: Mono<Request> here????
        
        return flux.flatMap { it ->
            val req = it as someObject
           //do something here........
        }
    }
}

【问题讨论】:

  • 你能试试这个吗?将切入点修改为 @Around("@annotation(someAnnotation)&amp;&amp; args(req,..)") ,然后将建议修改为 fun &lt;T&gt; getResponse(joinPoint: ProceedingJoinPoint, req : Mono&lt;Request&gt; ): Mono&lt;T&gt; 。您应该能够在req 参考中获取参数。我是一个 java 人,这是基于我如何阅读你的 Kotlin 代码。
  • 想法是在切入点添加一个参数,在advice方法中引用相同
  • 试过了,但得到了 java.lang.IllegalArgumentException: 警告与此类型名称不匹配:req [Xlint:invalidAbsoluteTypeName]。在应用程序启动时
  • 能否请您用您尝试过的内容更新问题。当参数不匹配时会发生此异常。
  • 更新了问题。

标签: java spring kotlin spring-webflux spring-aop


【解决方案1】:

修改方面代码将解决问题。 OP 想要为具有多个参数的方法提供建议,并且要提供给建议主体的所需参数将是最后一个参数。

这里通过cmets总结分辨率。

@Aspect
@Component
@Order(0)
class SomeAspect{
    @Suppress("UNCHECKED_CAST")
    @Around("@annotation(someAnnotation) && args(..,apiReq)")
    @Throws(Throwable::class)
    fun <T> getResponse(joinPoint: ProceedingJoinPoint,apiReq : Mono<T>): Mono<T> {
        
        val target = joinPoint.target
        val methodSignature = joinPoint.signature as MethodSignature
        val method: Method = methodSignature.method

        val flux: Mono<T> = joinPoint.proceed() as Mono<T>
        val methodArgs = joinPoint.args


        //.. apiReq will refer to the required argument
        return flux.flatMap { it ->
            val req = it as someObject
           //do something here........
        }
    }
}

【讨论】:

    猜你喜欢
    • 2020-10-22
    • 1970-01-01
    • 2019-10-06
    • 2017-11-19
    • 2018-11-17
    • 2020-08-12
    • 1970-01-01
    • 1970-01-01
    • 2019-01-17
    相关资源
    最近更新 更多