java aop 获取参数名,Java AOP JoinPoint不获取参数名称

I'm using Java Spring Mvc and Spring AOP to find the parameter names from the user.

I have a controller which get parameters from user and call a service.

I have an aspect that running before the service.

The aspect should check if username and apiKey parameters are exist.

Here is my code :

Controller :

@RequestMapping(method = RequestMethod.POST, produces=MediaType.APPLICATION_JSON_VALUE)

public @ResponseBody String getDomainWithFoundIn(@RequestParam (value="domain") String domain, @RequestParam (value="user") String user, @RequestParam (value="apiKey") String apiKey) throws JsonGenerationException, JsonMappingException, IOException {

return domainService.getDomainDataWithFoundIn(domain, user, apiKey);

}

Domain Service Interface :

public interface IDomainService {

public String getDomainDataWithFoundIn(String domainStr, String user, String apiKey);

}

DomainService :

@Override

@ApiAuthentication

public String getDomainDataWithFoundIn(String domainStr, String user, String apiKey) {

//Do stuff here

}

And my AOP class :

@Component

@Aspect

public class AuthAspect {

@Before("@annotation(apiAuthentication)")

public void printIt (JoinPoint joinPoint, ApiAuthentication apiAuthentication) throws NoAuthenticationParametersException, UserWasNotFoundException {

final MethodSignature signature = (MethodSignature) joinPoint.getSignature();

final String[] parameterNames = signature.getParameterNames();

**//parameterNames is null here.**

}

In this case, I'd expect to get on my aspect the "domain", "user" and "apiKey" parameter names.

Any idea what am i missing here ?

Thanks,

Or.

解决方案

As I've said in above comment, depending on proxy type you can or can't have access to parameter names. If your bean implements interface, the JDK proxy will be created by spring, and in this kind of proxy MethodSignature.getParameterNames() is null. If your bean doesn't implement interface, CGLIB proxy is created, where MethodSignature.getParameterNames() is filled.

If you can, you may switch to CGLIB proxy bean by removing bean interfaces and it should work.

I'm struggling with the same now, and I can't remove interfaces. I figured out different solution for this. On the interface I can mark my parameters by some custom annot:

interface MyInterface {

void myMetod(@ParamName("foo") Object foo, @ParamName("bar") Object bar);

}

Now in AOP proxy I can get this information in:

MethodSignature.getMethod().getParameterAnnotations()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java AOP 中,无法直接通过 `JoinPoint` 获取 POST 参数。`JoinPoint` 接口提供了一组方法来获取方法的相关信息,如方法名、参数等,但它不直接提供获取 HTTP 请求参数的功能。 要获取 POST 参数,你可以使用 Spring AOP 结合 Spring MVC 的功能来实现。在 Spring MVC 中,可以使用 `HttpServletRequest` 对象来获取请求参数。通过将 `HttpServletRequest` 作为切面方法的参数,在切面方法中可以通过它来获取 POST 参数。下面是一个简单的示例: ```java import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; @Aspect @Component public class MyAspect { @Before("execution(* com.example.MyController.myMethod(..)) && args(.., request)") public void beforeMethod(JoinPoint joinPoint, HttpServletRequest request) { // 获取 POST 参数 String param1 = request.getParameter("param1"); String param2 = request.getParameter("param2"); // 处理参数 // ... } } ``` 上述代码中,`MyController` 是你的控制器类,`myMethod` 是你想要拦截的方法。在切面方法 `beforeMethod` 中,通过将 `HttpServletRequest` 作为参数,并使用 `request.getParameter` 方法来获取 POST 参数。 需要注意的是,这种方式仅适用于 Spring MVC,并且需要确保你的项目中已经配置了正确的请求处理器和拦截器。另外,如果你使用的是其他 Java Web 框架,可以根据框架提供的方式获取 POST 参数。 希望这能帮到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值