I am facing an issue with Spring mvc annotation @Controlleradvice. I have 2 controller classes: UserGapsController and RegistrationBaseController Both classes use
@Controlleradvice annotation has to be used when @Modelattribute is used at method level. So i am having a method annotated with @Modelattribute in both classes. Now problem is when i am using @Controlleradvice in just UserGapsController.java , application runs fine , when i use @Controlleradvice in RegistrationBaseController.java also , it breaks down at runtime with following error:
error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.requestmappinghandler: invocation of init method failed:nested exceotion is org.springframework.beans.factory.BeanCreationException :Error creating bean with name 'userGapsController' : Scope 'session' is not active for current thread
What is the reason for this error , can not we have 2 @Controlleradvice annotated classes ? When I comment @Controlleradvice in RegistrationBaseController.java , it executes fine then.
You trying to have multiple @ControllerAdvice
classes that handle different exceptions.
You can use Order over controllerAdvice like this
@ControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
public class RegistrationExceptionHandler {
//...
}
and
@ControllerAdvice
@Order(Ordered.LOWEST_PRECEDENCE) // or any int value
public class UserGapsExceptionHandler {
//...
}