java - Webflux-Aop :Get method request(Mono) in Aspect class

标签 java spring kotlin spring-webflux spring-aop

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

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

方法

@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........
        }
    }
}

最佳答案

对方面代码进行以下修改将解决该问题。 OP 想要建议一个具有多个参数的方法,并且建议主体可用的所需参数将是最后一个参数。

通过此处的评论总结决议。

@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........
        }
    }
}

关于java - Webflux-Aop :Get method request(Mono) in Aspect class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68569738/

相关文章:

java - 如何在 Microsoft Azure 网站上更改 jvm 参数

android - 寻找一种 GC 友好的方式来频繁替换子字符串

kotlin - 从coroutineScope内部创建actor会阻塞线程,但是与CoroutineScope扩展函数创建的actor却不同

java - Spring Boot Embedded Kafka无法连接

java - 通过显式 SSL/TLS 在 FTP 上登录成功但无法浏览文件

java - Mockito - 是否有 "value not in List"的匹配器?

java - 为什么会出现此错误? "bad operand types for binary operator ' >'"

java - JGoodies 内存泄漏?? - 当 Spring 尝试显示 View 时出现内存不足异常

java - Spring (Java): Aspect is not triggered in non linear class hierarchy

java - 获取 Spring Servlet 应用程序上下文