我 MVC Exceptions 博客中涵盖的内容演示
mvn exec:java or mvn spring-boot:run to run it, then goto http://localhost:8080.pom-war.xmlThis application demos most of the points covered on my MVC Exceptions blog:
The most significant files are:
Controller with @ExceptionHandler methods
src/main/java/demo1/web/ExceptionHandlingController.javaController relying on a ControllerAdvice to handle its exceptions.
src/main/java/demo2/web/ControllerWithoutExceptionHandlers.java
ExceptionHandlingController (so they all throw exceptions) but no handler methods.src/main/java/demo2/web/GlobalControllerExceptionHandler.java
@ControllerAdvice class with all the same handlers as ExceptionHandlingController, 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.javasrc/main/java/demo3/web/ExceptionThrowingController.javasrc/main/java/demo3/web/SwitchableSimpleMappingExceptionResolver.javasrc/main/java/demo3/web/ExceptionThrowingController.java/simpleMappingExceptionResolver/on and
/simpleMappingExceptionResolver/off for switching the resolver on/off.ReturnOrRedirectControllersrc/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.javasrc/main/java/org/springframework/dao/DataAccessException.javasrc/main/java/org/springframework/dao/DataIntegrityViolationException.javaThe 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.javademo-config (default), java-config, xml-config.src/main/java/demo/main/Profiles.javasrc/main/java/demo/config/ExceptionConfiguration.javaSimpleMappingExceptionResolver. Only used if the java-config profile is active.src/main/resources/mvc-configuration.xmlExceptionConfiguration. Also sets up a SimpleMappingExceptionResolver. Only used if the xml-config profile is active.src/main/java/demo/config/ResponseDataControllerAdvicesrc/main/java/demo/utils/BeanLogger.javadebug=true.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.htmlWeb 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 5pom.xml
mvn package to create an executable JAR with embedded Tomcat.java -jar target/mvc-exceptions-2.0.1-RELEASE.jarpom-war.xml - If you prefer to build a traditional WAR file instead of an executable JAR.
mvn -f pom-war.xml packageNot used by the application, but provided as sample code.
src/main/java/demo/example/ExampleExceptionHandlerExceptionResolver.javasrc/main/java/demo/example/ExampleSimpleMappingExceptionResolver.javaMySimpleMappingExceptionResolver in the blog article).暂无开放 Issues,或尚未同步最近议题。