redirecting using aspectJ

@Aspect @Component public class myAspect { //Controller perspective (getting all method that have myAnnotation and the RequestMapping annotation) @Around("within(@org.myAnnotation *) && @annotation(org.springframework.web.bind.annotation.RequestMapping)") public ModelAndView classPointcut(ProceedingJoinPoint joinPoint) throws Throwable { //Getting the accessed url inside the advice MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); Method method = methodSignature .getMethod(); String url = method.getAnnotation(RequestMapping.class).value()[0]; //redirecting or not depending on some condition if(condition){ return new ModelAndView("redirect:/redirectUrl") } return (ModelAndView) joinPoint.proceed(); } //Method perspective @Around("@annotation(org.myAnnotation)") public ModelAndView methodPointcut(ProceedingJoinPoint jointPoint) throws Throwable{ //Getting the accessed url inside the advice MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); Method method = methodSignature .getMethod(); String url = method.getAnnotation(RequestMapping.class).value()[0]; //redirecting or not depending on some condition if(condition){ return new ModelAndView("redirect:/redirectUrl") } return (ModelAndView) joinPoint.proceed(); } }
How to redirect to another url using aspectJ and Spring based on some condition.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.