Вы можете получить сеанс из вашего @ControllerAdvice, используя:
Вариант 1:
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
HttpSession session = requeset.getSession(true);//true will create if necessary
Вариант 2:
@Autowired(required=true)
private HttpServletRequest request;
Вариант 3:
@Context
private HttpServletRequest request;
Вот пример o f как я разработал аспект контроллера, который перехватывает все методы конечной точки контроллера:
@Component
@Aspect
class ControllerAdvice{
@Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
void hasRequestMappingAnnotation() {}
@Pointcut("execution(* your.base.package..*Controller.*(..))")
void isMethodExecution() {}
/**
* Advice to be executed if this is a method being executed in a Controller class within our package structure
* that has the @RequestMapping annotation.
* @param joinPoint
* @throws Throwable
*/
@Before("hasRequestMappingAnnotation() && isMethodExecution()")
void beforeRequestMappedMethodExecution(JoinPoint joinPoint) {
String method = joinPoint.getSignature().toShortString();
System.out.println("Intercepted: " + method);
//Now do whatever you need to
}
}
Почему бы не использовать перехватчики для этого? – JSONStatham
@ JSONStatham - отличная идея! –