Home > Mobile >  Do java validation annotations run before AOP
Do java validation annotations run before AOP

Time:01-04

I'm new in Spring Boot AOP.

Does an AOP method annotated with @Before run before java validation annotations (such as @NotNull)?

I have some other custom validations that need to run for every request but I need to run these validations after java validation annotations run.

Which one will run first?

my Controller:

@RestController
@RequestMapping("/users")
public class UserController {

    private final UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    @PostMapping(value = "")
    public List<User> getAllUsers(@Valid @RequestBody User user) {
        return userService.getAllUsers();
    }
}

and my advice:

@Aspect
@Component
public class AspectConfig {

    @Pointcut(value = "within(@org.springframework.web.bind.annotation.RestController *)")
    public void restControllers() {

    }

    @Before(value = "restControllers()")
    public void logRequest(JoinPoint joinPoint) { 
         ...
    }

}

CodePudding user response:

Does an AOP method annotated with @Before run before java validation annotations

No, it runs afterwards, just like you wish. See also this question. So you should be all set. Your logging advice should only be triggered if validation was successful, because only then the target method will be called.

You can implement a HandlerInterceptor if you wish to log/do something on the request level before validators kick in, see here.

  •  Tags:  
  • Related