Grails 3.0.11 AOP Annotations for Preprocessing Command Objects Before Controller Methods

I am trying to get the arguments of a grails controller method using the annotation and Aspect that is executed before the method. The aspect handler executes correctly, but I cannot access the argument (which implements grails.validation.Validateable ), the argument list is empty.

experiment.aspect.validated

 package experiment.aspect import java.lang.annotation.ElementType import java.lang.annotation.Retention import java.lang.annotation.RetentionPolicy import java.lang.annotation.Target /** * Created by Vaggelis on 10/13/2016. */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @interface Validated { } 

experiment.aspect.ValidatedAspect

 package experiment.aspect import grails.validation.ValidationException import org.aspectj.lang.JoinPoint import org.aspectj.lang.annotation.Aspect import org.aspectj.lang.annotation.Before /** * Created by Vaggelis on 10/13/2016. */ @Aspect class ValidatedAspect { @Before("@annotation(experiment.aspect.Validated)") public void preValidate(JoinPoint point) throws ValidationException{ println "parameters ${point.getArgs()}" } } 

conf.spring.resources

 import experiment.aspect.ValidatedAspect // Place your Spring DSL code here beans = { validatedAspect(ValidatedAspect) } 

controllers.experiment.TestController

 package experiment import experiment.aspect.Validated class TestController extends BaseController { static responseFormats = ['json'] @Validated def login(LoginCommand loginCommand){ println "Validated" ... } } 

experiment.LoginCommand

 package experiment /** * Created by Vaggelis on 9/14/2016. */ import grails.validation.Validateable class LoginCommand implements Validateable { // String id String name static constraints = { name blank: false } } 

I get the following output, which means that the aspect handler method is executed before the controller method, but it does not receive arguments.

 parameters [] Validated 
+5
source share
1 answer

You do not see the arguments, because they are not.

In order to maintain data binding and simplify the order when the servlet decides which controller and method should be called, the AST transform creates a new method with the same name and no arguments for all action methods that have any arguments. The zero-arg method is the one originally called by the servlet, and it has the logic added by the transform to make data binding calls. After that, it calls your parameterized method with parameters converted to int / long / boolean / command / etc objects.

The no-arg method will also be annotated using @grails.web.Action and the arg types specified in the commandObjects attribute. This will help you find another method, but it's actually simple, because you will get a compiler error if you declare overloaded public methods.

Having said all this, you probably will not want to use this approach, even if it really works - there are already two standard methods for intercepting calls to controller actions, Grails filters (now interceptors in 3.x) and servlet filters. Grails filters and interceptors make it trivial to inspect and it is not necessary to add, change or delete request parameters, and if you return a false call before, you will stop Grails handling the request (for example, because you visualized it or sent a redirect, and etc.)

+2
source

Source: https://habr.com/ru/post/1258166/


All Articles