- Last updated July 2024
- This project is built using release 2.7.18 of Spring Boot, Java 8 and Spring 5.3.31 - see pom.xml.
- Web pages use Thymeleaf 3.0.15 and Bootstrap 4.5.3.
- The POM builds a JAR file, not a WAR, so you must run it as a Java application. Use
mvn exec:javaormvn spring-boot:runto run it, then gotohttp://localhost:8080. - If you wish to build a WAR, see
pom-war.xml
This application demos most of the points covered on my MVC Exceptions blog:
- Local copy is here: blog.md.
- Original blog: https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc.
- November 2013: V1
- October 2014: V2
- April 2018: V2.0.1
- January 2021: V2.1.0
- January 2021: V2.1.1
The most significant files are:
Controller with @ExceptionHandler methods
src/main/java/demo1/web/ExceptionHandlingController.java- A controller that raises exceptions and provides its own handlers to catch and process them.
Controller relying on a ControllerAdvice to handle its exceptions.
-
src/main/java/demo2/web/ControllerWithoutExceptionHandlers.java- A controller with all the same handlers as
ExceptionHandlingController(so they all throw exceptions) but no handler methods.
- A controller with all the same handlers as
-
src/main/java/demo2/web/GlobalControllerExceptionHandler.java@ControllerAdviceclass with all the same handlers asExceptionHandlingController, but they would apply to all controllers.
Exception handling using a SimpleMappingExceptionResolver. When running in demo mode (profile is set to demo-config, which is setup by default), it defines a SimpleMappingExceptionResolver subclass that can be enabled (Demo 3) or disabled (Demo 4) to show the difference.
src/main/java/demo3/config/DemoExceptionConfiguration.java- Java configuration to setup the beans for this demo.
src/main/java/demo3/web/ExceptionThrowingController.java- Controller used by the demo.
src/main/java/demo3/web/SwitchableSimpleMappingExceptionResolver.java- The resolver subclass described above.
src/main/java/demo3/web/ExceptionThrowingController.java- Controller that provides
/simpleMappingExceptionResolver/onand/simpleMappingExceptionResolver/offfor switching the resolver on/off.
- Controller that provides
ReturnOrRedirectController- Controller highlighting how Spring Boot implements its error-page mechanism.
src/main/java/demo/exceptions/CustomException.javasrc/main/java/demo/exceptions/DatabaseException.javasrc/main/java/demo/exceptions/InvalidCreditCardException.javasrc/main/java/demo/exceptions/OrderNotFoundException.javasrc/main/java/demo/exceptions/UnhandledException.java- Custom exceptions - see blog for usage.
src/main/java/org/springframework/dao/DataAccessException.java- Example of a predefined annotation, copied from Spring.
src/main/java/org/springframework/dao/DataIntegrityViolationException.java- Example of a predefined annotation, copied from Spring.
The Demo configuration profile is useful for this demo application, but not typical. So two other profiles are provided to configure a SimpleMappingExceptionResolver in a more typical way using either Java configuration or an XML bean file.
src/main/java/demo/main/Main.java- Main entry point for the application. Can run as a Java application (using an embedded Tomcat container) or as a WAR inside a container. Sets a few initialization properties and Spring Bean profile to use. Available profiles are
demo-config(default),java-config,xml-config.
- Main entry point for the application. Can run as a Java application (using an embedded Tomcat container) or as a WAR inside a container. Sets a few initialization properties and Spring Bean profile to use. Available profiles are
src/main/java/demo/main/Profiles.java- The Spring Bean profiles used in the application.
src/main/java/demo/config/ExceptionConfiguration.java- Java configuration class to setup a
SimpleMappingExceptionResolver. Only used if thejava-configprofile is active.
- Java configuration class to setup a
src/main/resources/mvc-configuration.xml- XML alternative to
ExceptionConfiguration. Also sets up aSimpleMappingExceptionResolver. Only used if thexml-configprofile is active.
- XML alternative to
src/main/java/demo/config/ResponseDataControllerAdvice- Controller advice that puts useful data into the model for every request.
src/main/java/demo/utils/BeanLogger.java- Simple BeanPostProcessor to log all beans created. Not required by the demo, but as Spring Boot does so much, it allows all the beans created to be easily logged. And a lot less output than enabling
debug=true.
- Simple BeanPostProcessor to log all beans created. Not required by the demo, but as Spring Boot does so much, it allows all the beans created to be easily logged. And a lot less output than enabling
All the views used, generated via Thymeleaf.
-
Error views
src/main/resources/templates/creditCardError.htmlsrc/main/resources/templates/databaseError.htmlsrc/main/resources/templates/databaseException.htmlsrc/main/resources/templates/error.htmlsrc/main/resources/templates/exceptionPage.htmlsrc/main/resources/templates/support.html
-
Web Pages
src/main/resources/templates/index.html- Home pagesrc/main/resources/templates/local.html- Demo 1src/main/resources/templates/global.html- Demo 2src/main/resources/templates/unannotated.html- Demo 3src/main/resources/templates/nohandler.html- Demo 4src/main/resources/templates/demo5.html- Demo 5
-
pom.xml- Maven POM - notice how short it is - Spring Boot does most of the work. However heed the comments in the file.
- Build in the usual way:
mvn packageto create an executable JAR with embedded Tomcat. - You can also run the demo using
java -jar target/mvc-exceptions-2.0.1-RELEASE.jar
-
pom-war.xml- If you prefer to build a traditional WAR file instead of an executable JAR.- Build using
mvn -f pom-war.xml package
- Build using
Not used by the application, but provided as sample code.
src/main/java/demo/example/ExampleExceptionHandlerExceptionResolver.java- Unused in the demo (to keep it simple), but implements the example discussed in the blog.
src/main/java/demo/example/ExampleSimpleMappingExceptionResolver.java- Unused in the demo (to keep it simple), but implements the example shown in the blog (called
MySimpleMappingExceptionResolverin the blog article).
- Unused in the demo (to keep it simple), but implements the example shown in the blog (called