WARNINGS:
- This project is built using the latest M5 snapshot release of Spring Boot.
- APIs may change and this project may not build.
- Check http://spring.io/spring-boot for snapshot, milestone and other releases.
- Update the pom.xml if necessary.
This application demos most of the points covered on my MVC Exceptions blog: https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc.
The files are:
pom.xml
- Maven POM - notice how much shorter it is - Spring Boot does most of the work. However heed the comments in the file.
src/main/java/demo1/exceptions/CustomException.java
src/main/java/demo1/exceptions/DatabaseException.java
src/main/java/demo1/exceptions/InvalidCreditCardException.java
src/main/java/demo1/exceptions/OrderNotFoundException.java
src/main/java/demo1/exceptions/UnhandledException.java
- Custom exceptions - see blog for usage.
src/main/java/demo1/main/LoggingUtilities.java
- Handy utility for setting logging levels - the code is put here to keep the classes that use it simpler.
src/main/java/demo1/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(s).
src/main/java/demo1/main/Profiles.java
- The Spring Bean profiles used in the application.
src/main/java/demo1/web/BeanLogger.java
- Simple BeanPostProcessor to log all beans created. Not required by the demo, but as Spring Boot is new, it allows all the beans created to be logged.
src/main/java/demo1/web/ControllerWithoutExceptionHandlers.java
- A controller with all the same handlers as
ExceptionHandlingController
(so they all throw exceptions) but no handler methods. Only configured if theglobal
profile is enabled.
- A controller with all the same handlers as
src/main/java/demo1/web/ExampleExceptionHandlerExceptionResolver.java
- Unused in the demo (to keep it simple), but implements the example discussed in the blog.
src/main/java/demo1/web/ExampleSimpleMappingExceptionResolver.java
- Unused in the demo (to keep it simple), but implements the example shown in the blog
(called
MySimpleMappingExceptionResolver
in the blog article).
- Unused in the demo (to keep it simple), but implements the example shown in the blog
(called
src/main/java/demo1/web/ExceptionConfiguration.java
- Java configuration class to setup a
SimpleMappingExceptionResolver
. Only used if thejava-config
profile is active.
- Java configuration class to setup a
src/main/java/demo1/web/ExceptionHandlingController.java
- A controller that raises exceptions and provided handlers to catch and process them. Only defined
if the
controller
profile is active.
- A controller that raises exceptions and provided handlers to catch and process them. Only defined
if the
src/main/java/demo1/web/ExtraThymeleafConfiguration.java
- Some extra configuration to allow Thymeleaf and JSP to co-exist in the same application. Spring-Boot
cannot do this out-of-the box because of the way
ThymeleafViewResover
works - see Javadoc for more.
- Some extra configuration to allow Thymeleaf and JSP to co-exist in the same application. Spring-Boot
cannot do this out-of-the box because of the way
src/main/java/demo1/web/GlobalControllerExceptionHandler.java
@ControllerAdvice
class with all the same handlers asExceptionHandlingController
, but they would apply to all controllers. Only configured if theglobal
profile is enabled.
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.
src/main/resources/mvc-configuration.xml
- XML alternative to
ExceptionConfiguration
. Also sets up aSimpleMappingExceptionResolver
. Only used if thexml-config
profile is active.
- XML alternative to
src/main/resources/templates/contacts.html
src/main/resources/templates/creditCardError.html
src/main/resources/templates/databaseError.html
src/main/resources/templates/error.html
src/main/resources/templates/exceptionPage.html
src/main/resources/templates/index.html
- All the views used, generated via Thymeleaf.
src/main/webapp/WEB-INF/support.jsp
- A JSP error page with exception details, including a stack-trace hidden in a comment. It can be revealed by viewing the Page Source (for example at the request of your Support team).