0
votes

I have an @Aspect and @Pointcut method annotated to fire @Before a @Controller request method, it seems to be matching (as I'm not getting any errors) but it is not firing my advice method at all. I changed my pointcut for testing purposes to be as specific as possible and am not getting any binding errors during application startup.

Here's my controller method (the class is com.x.y.z.MyController):

@RequestMapping(method = RequestMethod.POST, produces = "application/json", consumes = "application/json")
@ResponseBody
public SubmissionResponse submitMethod(@Valid @RequestBody final SubmissionRequest request, HttpServletRequest httpRequest, BindingResult result)
{
    if (result.hasErrors()) { throw new BadRequestException(result); }

    //  ... do stuff ...
}

Here's the Aspect class:

@Aspect
@Component
public class RequestValidatingAspect
{
    private static final Logger LOGGER = Logger.getLogger(RequestValidatingAspect.class);

    @Inject
    private ClientService clientService;

    @Inject
    private AccountService accountService;

    @Pointcut("execution(* com.x.y.z.MyController.submitMethod(*.SubmissionRequest,*.HttpServletRequest,*.BindingResult)) && args(request, httpRequest, result)")
    private void requestValidation(SubmissionRequest request, HttpServletRequest httpRequest, BindingResult result) {} 

    @Before("requestValidation(request,httpRequest,result)")    
    public void theAdvice(SubmissionRequest request, HttpServletRequest httpRequest, BindingResult result) throws Throwable
    {
        System.out.println("Before - The Advice");
        LOGGER.info("Entering The Advice!");

        if(result.hasErrors()){ throw new BadRequestException(result); }
        // ... do stuff ...

        LOGGER.info("Exiting - The Advice!");
        return;
    }
}
2
Can we see your context? - Sotirios Delimanolis

2 Answers

0
votes

Turns out that the line @Pointcut wasn't entirely correct. Changing:

.. submitMethod(*.SubmissionRequest,*.HttpServletRequest,*.BindingResult) ..

to submitMethod(..) or using a fully qualified class name for each of the three objects with the same args filter allowed the advice to lock in on the different methods I wanted advised. I Wound up changing my approach a little bit though and created a custom annotation to directly signal which methods I wanted advised and wound up with this final pointcut:

@Pointcut("@annotation(com.x.y.z.annotation.SpecificTypeOfRequestValidation) && args(request, httpRequest, result)")

0
votes

If you are using Spring 4 then all you can use @ControllerAdvice annotation to centralize all the request validations which would seem more helpful from maintainance point of view.