Spring Exception Handler - show error page for “500 javax.servlet.ServletException: Servlet.init()...
up vote
0
down vote
favorite
I have several Spring Services in my application which are independent from each other. In an init method of the services, annotated with @PostConstruct, some dependencies to external services will be initialized. However if this initialization process fails an error screen with message 500 javax.servlet.ServletException: Servlet.init() exception will be displayed with root cause BeanCreationException of the service who's @PostConstruct init could not be executed successfully.
I understand that in case of a Bean initialization failure the application is not consistent anymore and therefore the whole app might not work as intended. However since the services are independent from the rest of the application I would like to catch this initialization exception and still access the application in some maintenance mode where you will get an error screen in case the page of the service, which could not be initialized, should be accessed.
I tried several approaches using a Global Controller Exception annotated with @ControllerAdvice or an @ExceptionHandler method but they wont get triggered. The error screen is displayed immediately.
Another try was using a Filter or a HandlerExceptionResolver, but both don't get triggered in this case.
After some research I found some discussions that this error is fatal and therefore cannot be handled or cached. Also it seem that the exception is thrown before the request is passed to the controller handle method or any configured filter, therefore the controller exception handler are not triggered as well.
My questions is now: Is there any other approach which I am still missing in order to handle BeanCreationExceptions? The final solution should start the application in some maintenance mode which offers an error screen when the failed Bean/Service should be accessed, or give some information about failed initialized services.
Thank in advance for anyone how has some hints or further explanation.
java spring
add a comment |
up vote
0
down vote
favorite
I have several Spring Services in my application which are independent from each other. In an init method of the services, annotated with @PostConstruct, some dependencies to external services will be initialized. However if this initialization process fails an error screen with message 500 javax.servlet.ServletException: Servlet.init() exception will be displayed with root cause BeanCreationException of the service who's @PostConstruct init could not be executed successfully.
I understand that in case of a Bean initialization failure the application is not consistent anymore and therefore the whole app might not work as intended. However since the services are independent from the rest of the application I would like to catch this initialization exception and still access the application in some maintenance mode where you will get an error screen in case the page of the service, which could not be initialized, should be accessed.
I tried several approaches using a Global Controller Exception annotated with @ControllerAdvice or an @ExceptionHandler method but they wont get triggered. The error screen is displayed immediately.
Another try was using a Filter or a HandlerExceptionResolver, but both don't get triggered in this case.
After some research I found some discussions that this error is fatal and therefore cannot be handled or cached. Also it seem that the exception is thrown before the request is passed to the controller handle method or any configured filter, therefore the controller exception handler are not triggered as well.
My questions is now: Is there any other approach which I am still missing in order to handle BeanCreationExceptions? The final solution should start the application in some maintenance mode which offers an error screen when the failed Bean/Service should be accessed, or give some information about failed initialized services.
Thank in advance for anyone how has some hints or further explanation.
java spring
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have several Spring Services in my application which are independent from each other. In an init method of the services, annotated with @PostConstruct, some dependencies to external services will be initialized. However if this initialization process fails an error screen with message 500 javax.servlet.ServletException: Servlet.init() exception will be displayed with root cause BeanCreationException of the service who's @PostConstruct init could not be executed successfully.
I understand that in case of a Bean initialization failure the application is not consistent anymore and therefore the whole app might not work as intended. However since the services are independent from the rest of the application I would like to catch this initialization exception and still access the application in some maintenance mode where you will get an error screen in case the page of the service, which could not be initialized, should be accessed.
I tried several approaches using a Global Controller Exception annotated with @ControllerAdvice or an @ExceptionHandler method but they wont get triggered. The error screen is displayed immediately.
Another try was using a Filter or a HandlerExceptionResolver, but both don't get triggered in this case.
After some research I found some discussions that this error is fatal and therefore cannot be handled or cached. Also it seem that the exception is thrown before the request is passed to the controller handle method or any configured filter, therefore the controller exception handler are not triggered as well.
My questions is now: Is there any other approach which I am still missing in order to handle BeanCreationExceptions? The final solution should start the application in some maintenance mode which offers an error screen when the failed Bean/Service should be accessed, or give some information about failed initialized services.
Thank in advance for anyone how has some hints or further explanation.
java spring
I have several Spring Services in my application which are independent from each other. In an init method of the services, annotated with @PostConstruct, some dependencies to external services will be initialized. However if this initialization process fails an error screen with message 500 javax.servlet.ServletException: Servlet.init() exception will be displayed with root cause BeanCreationException of the service who's @PostConstruct init could not be executed successfully.
I understand that in case of a Bean initialization failure the application is not consistent anymore and therefore the whole app might not work as intended. However since the services are independent from the rest of the application I would like to catch this initialization exception and still access the application in some maintenance mode where you will get an error screen in case the page of the service, which could not be initialized, should be accessed.
I tried several approaches using a Global Controller Exception annotated with @ControllerAdvice or an @ExceptionHandler method but they wont get triggered. The error screen is displayed immediately.
Another try was using a Filter or a HandlerExceptionResolver, but both don't get triggered in this case.
After some research I found some discussions that this error is fatal and therefore cannot be handled or cached. Also it seem that the exception is thrown before the request is passed to the controller handle method or any configured filter, therefore the controller exception handler are not triggered as well.
My questions is now: Is there any other approach which I am still missing in order to handle BeanCreationExceptions? The final solution should start the application in some maintenance mode which offers an error screen when the failed Bean/Service should be accessed, or give some information about failed initialized services.
Thank in advance for anyone how has some hints or further explanation.
java spring
java spring
edited Oct 25 at 12:41
asked Oct 25 at 9:23
Agnes Fröschl
14
14
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
I found some workaround which might help someone else facing the same issue, so I would like to share my approach.
I ended up using a combination of the @Conditional annotation, which manages the creation of beans and a customer error page, configured in web.xml.
Basically, I used @Conditional on every service which depends on external resources. The Condition checks if any needed external dependencies can be initialized.
If the matches method of the Condition returns true the service annotated with my Condition class will be created otherwise spring ignores the creation of the bean for that service.
Here is an example of my Condition implementation (I just execute in matches what is contained in the @PostConstruct init method of the service):
public class ModuleCondition implements Condition {
@Override
public boolean matches(ConditionContext arg0, AnnotatedTypeMetadata arg1) {
if (/*check external dependencies*/)
return true;
else
return false;
}
}
and here is the an example of using the Condition:
@Service
@Conditional(value = ModuleCondition.class)
public class MyService {
...
}
In order to handle the 404 error on case the interface of the service which could not be initialized I used a customer error page and a CustomerErrorController. (I followed the following tutorial here)
So, in case a service cannot be initialized, which got caused by a failed initialization of external resources, the user will be forwarded to a customer error page informing the user that the accessed module is currently not available.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
I found some workaround which might help someone else facing the same issue, so I would like to share my approach.
I ended up using a combination of the @Conditional annotation, which manages the creation of beans and a customer error page, configured in web.xml.
Basically, I used @Conditional on every service which depends on external resources. The Condition checks if any needed external dependencies can be initialized.
If the matches method of the Condition returns true the service annotated with my Condition class will be created otherwise spring ignores the creation of the bean for that service.
Here is an example of my Condition implementation (I just execute in matches what is contained in the @PostConstruct init method of the service):
public class ModuleCondition implements Condition {
@Override
public boolean matches(ConditionContext arg0, AnnotatedTypeMetadata arg1) {
if (/*check external dependencies*/)
return true;
else
return false;
}
}
and here is the an example of using the Condition:
@Service
@Conditional(value = ModuleCondition.class)
public class MyService {
...
}
In order to handle the 404 error on case the interface of the service which could not be initialized I used a customer error page and a CustomerErrorController. (I followed the following tutorial here)
So, in case a service cannot be initialized, which got caused by a failed initialization of external resources, the user will be forwarded to a customer error page informing the user that the accessed module is currently not available.
add a comment |
up vote
0
down vote
I found some workaround which might help someone else facing the same issue, so I would like to share my approach.
I ended up using a combination of the @Conditional annotation, which manages the creation of beans and a customer error page, configured in web.xml.
Basically, I used @Conditional on every service which depends on external resources. The Condition checks if any needed external dependencies can be initialized.
If the matches method of the Condition returns true the service annotated with my Condition class will be created otherwise spring ignores the creation of the bean for that service.
Here is an example of my Condition implementation (I just execute in matches what is contained in the @PostConstruct init method of the service):
public class ModuleCondition implements Condition {
@Override
public boolean matches(ConditionContext arg0, AnnotatedTypeMetadata arg1) {
if (/*check external dependencies*/)
return true;
else
return false;
}
}
and here is the an example of using the Condition:
@Service
@Conditional(value = ModuleCondition.class)
public class MyService {
...
}
In order to handle the 404 error on case the interface of the service which could not be initialized I used a customer error page and a CustomerErrorController. (I followed the following tutorial here)
So, in case a service cannot be initialized, which got caused by a failed initialization of external resources, the user will be forwarded to a customer error page informing the user that the accessed module is currently not available.
add a comment |
up vote
0
down vote
up vote
0
down vote
I found some workaround which might help someone else facing the same issue, so I would like to share my approach.
I ended up using a combination of the @Conditional annotation, which manages the creation of beans and a customer error page, configured in web.xml.
Basically, I used @Conditional on every service which depends on external resources. The Condition checks if any needed external dependencies can be initialized.
If the matches method of the Condition returns true the service annotated with my Condition class will be created otherwise spring ignores the creation of the bean for that service.
Here is an example of my Condition implementation (I just execute in matches what is contained in the @PostConstruct init method of the service):
public class ModuleCondition implements Condition {
@Override
public boolean matches(ConditionContext arg0, AnnotatedTypeMetadata arg1) {
if (/*check external dependencies*/)
return true;
else
return false;
}
}
and here is the an example of using the Condition:
@Service
@Conditional(value = ModuleCondition.class)
public class MyService {
...
}
In order to handle the 404 error on case the interface of the service which could not be initialized I used a customer error page and a CustomerErrorController. (I followed the following tutorial here)
So, in case a service cannot be initialized, which got caused by a failed initialization of external resources, the user will be forwarded to a customer error page informing the user that the accessed module is currently not available.
I found some workaround which might help someone else facing the same issue, so I would like to share my approach.
I ended up using a combination of the @Conditional annotation, which manages the creation of beans and a customer error page, configured in web.xml.
Basically, I used @Conditional on every service which depends on external resources. The Condition checks if any needed external dependencies can be initialized.
If the matches method of the Condition returns true the service annotated with my Condition class will be created otherwise spring ignores the creation of the bean for that service.
Here is an example of my Condition implementation (I just execute in matches what is contained in the @PostConstruct init method of the service):
public class ModuleCondition implements Condition {
@Override
public boolean matches(ConditionContext arg0, AnnotatedTypeMetadata arg1) {
if (/*check external dependencies*/)
return true;
else
return false;
}
}
and here is the an example of using the Condition:
@Service
@Conditional(value = ModuleCondition.class)
public class MyService {
...
}
In order to handle the 404 error on case the interface of the service which could not be initialized I used a customer error page and a CustomerErrorController. (I followed the following tutorial here)
So, in case a service cannot be initialized, which got caused by a failed initialization of external resources, the user will be forwarded to a customer error page informing the user that the accessed module is currently not available.
answered yesterday
Agnes Fröschl
14
14
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52985810%2fspring-exception-handler-show-error-page-for-500-javax-servlet-servletexcepti%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password