How do I catch all exceptions to Spring/Spring Boot 2.x?
I built my Spring boot 2.x application using this demo:
https://spring.io/guides/gs/spring-boot/
The problem I’m having is that when an exception occurs in Spring/Spring boot, it prints to standard output. I don’t want that. I want to capture them and do other processing, such as recording them. I can catch exceptions for my code, but I can’t catch exceptions for Spring/Spring boot. So, how do you catch all Spring/Spring Boot 2.x exceptions in order to handle them? Is there an easy way to do this, like a generic exception catcher? Can anyone tell me some code?
My code:
1。 Example .java
package example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplication
public class Example extends SpringBootServletInitializer {
public static void main(String[] args) throws Exception {
SpringApplication.run(Example.class, args);
}
}
2。 ExampleController.java
package example;
import org.springframework.stereotype.Controller;
import org.springframework.http.HttpStatus;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class ExampleController {
@GetMapping ("/example")
public ModelAndView example()
{
MyData data= new MyData();
data.setName("Example");
data.setVersion("1.0");
ModelAndView model = new ModelAndView("page");
model.addObject("page", data);
return model;
}
}
3。 GeneralExceptionHandler.java
package example;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
@ControllerAdvice
class GeneralExceptionHandler {
@ExceptionHandler(Exception.class)
public void handleException() {
System.out.println("Exception handler");
}
}
4。 My data .java
package example;
import lombok. Data;
@Data
public class MyData {
private String name;
private String version;
}
5。 Page .jsp
<! DOCTYPE html>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<form:form id="myForm" class="form-horizontal"
modelAttribute="page">
<form:label id="name" class="control-label" path="name" for="name">
${page.name}</form:label>
<!-- Error introduced on purpose to force exception. "version123" should be just "version" -->
<form:label id="version" class="control-label" path="version" for="version">
${page.version123}</form:label>
</form:form>
</html>
Solution
You can use @ExceptionHandler annotations to catch specific exceptions, for
which you can simply use @ExceptionHandler
annotations methods within the Controller and provide them with specific exceptions, for example:
@ExceptionHandler(DataIntegrityViolationException.class)
public void handleException(){
do some thing here
}
The limitation of this approach is that it only handles exceptions thrown by @RequestMapping
that declare @ExceptionHandler
. To avoid this limitation, you can use Controller suggestions, which allow you to use exactly the same exception handling techniques, but apply them to the entire application, such as using Controller recommendations:
@ControllerAdvice
class GeneralExceptionHandler {
@ExceptionHandler(DataIntegrityViolationException.class)
public void handleException() {
Do some thing here
}
}
Tip: If you want to catch all checked exceptions, you can use @ExceptionHandler (Exception.class
).