Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get specific header parameter using Spring AOP?

I have created Spring Boot REST API where all endpoint will have header parameter "sessionGuid". I would like to print that sessionGuid using AOP.

@Before("PointcutDefinition.controllerLayer()")
  public void beforeAdvice(JoinPoint joinPoint)
  {
    Object[] signatureArgs = joinPoint.getArgs();
    for (Object signatureArg : signatureArgs)
    {
      System.out.println("Arg: " + signatureArg);
    }
  }

The above code is printing all the arguments i.e. if my URL is

{{base-url}}/v1/login/users/SOMENAME/status it is printing both SOMENAME (path variable) and "sessionGuid" value. I just want to print the value from the header parameter "sessionGuid".

joinPoint.getArgs(); is returning an array. I don't want to print something like arg[1] since sessionGuid may be the 3rd or 4th argument in different operations.

Is there a way by which i can print only "sessionGuid" from the header.

like image 871
Thiagarajan Ramanathan Avatar asked Dec 28 '18 19:12

Thiagarajan Ramanathan


People also ask

What is JoinPoint proceed ()?

In summary, joinPoint. proceed(); means that you are calling the set method, or invoking it. Follow this answer to receive notifications. edited Jun 20, 2020 at 9:12.

What is pointcut in Spring AOP?

In Spring AOP, a join point always represents a method execution. A pointcut is a predicate that matches the join points, and the pointcut expression language is a way of describing pointcuts programmatically.

Does Spring AOP use AspectJ?

In Spring AOP, aspects are implemented using regular classes (the schema-based approach) or regular classes annotated with the @Aspect annotation (the @AspectJ style). Join point: a point during the execution of a program, such as the execution of a method or the handling of an exception.


1 Answers

if you are looking for a solution to your problem you can use directly RequestContextHolder.

    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    String header = request.getHeader("sessionGuid")

you can also use reflection API if you want to be more generic.

like image 71
stacker Avatar answered Sep 30 '22 07:09

stacker