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.










share|improve this question




























    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.










    share|improve this question


























      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.










      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 25 at 12:41

























      asked Oct 25 at 9:23









      Agnes Fröschl

      14




      14
























          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.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            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
































            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.






            share|improve this answer

























              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.






              share|improve this answer























                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.






                share|improve this answer












                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.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered yesterday









                Agnes Fröschl

                14




                14






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    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




















































































                    Popular posts from this blog

                    How to pass form data using jquery Ajax to insert data in database?

                    National Museum of Racing and Hall of Fame

                    Guess what letter conforming each word