When we are doing Web applications, it is very common for errors to occur during request processing. Spring Boot provides a default mapping: When an exception is thrown in processing, it is /error
forwarded to the request for processing, and the request has a global error page to display the exception content. Springboot default error page effect is as follows:
Although the default error mapping is implemented in Spring boot, in practice, your error page is not friendly to the user, and we usually need to implement our own exception hints.
Instead of @ControllerAdvice
defining each controller individually, we can define a uniform exception-handling class by using it. To @ExceptionHandler
define the type of exception the function is targeting, and finally map the exception object and the request URL to the error.html
@ControllerAdvice Public classGlobalexceptionhandler {@ExceptionHandler (value= Exception.class) PublicModelandview Defaulterrorhandler (httpservletrequest req, Exception e) throws Exception {Modelandview Mav =NewModelandview (); Mav.addobject ("Exception", E); Mav.addobject ("URL", Req.getrequesturl ()); Mav.setviewname ("Error"); returnMav; }}
Implementation error.html
page display: Created in the templates
directory error.html
, the requested URL and the exception object's message output.
<! DOCTYPE html>"en"> <meta charset=" UTF-8" /> <title> Unified exception handling </title>"${url}"></div> <div th : text="${exception.message}"></div></body>
To launch the app, Access: http://localhost:8080/hello
, you can see the following error prompt page.
By implementing the above, we only need to Controller
throw in the Exception
, of course we may have a variety of different Exception
. Then, in the @ControllerAdvice
class, Exception
match the @ExceptionHandler
error mapping and processing according to the exception type that is configured in the specific type match that is thrown.
Return JSON formatIn the example above, @ControllerAdvice
mapping to different error handling pages by unifying the different exception is defined. And when we want to implement the RESTful API, the error returned is JSON-formatted data, not HTML pages, which we can easily support.
Essentially, @ExceptionHandler
@ResponseBody
you can convert the contents of the return of the handler function into JSON format simply by adding it later.
Create a unified JSON return object, code: Message type, message: Content, URL: Requested url,data: Request returned data
Public classErrorinfo<t> { Public StaticFinal Integer OK =0; Public StaticFinal Integer ERROR = -; PrivateInteger Code; PrivateString message; PrivateString URL; PrivateT data; //omit getter and setter}
Creating Exception Handling Classes
@ControllerAdvice Public classGlobalexceptionhandler {@ExceptionHandler (value= MyException.class) @ResponseBody PublicErrorinfo<string>Jsonerrorhandler (httpservletrequest req, myexception e) throws Exception {ErrorInfo<String> r =NewErrorinfo<>(); R.setmessage (E.getmessage ()); R.setcode (Errorinfo.error); R.setdata ("Some Data"); R.seturl (Req.getrequesturl (). toString ()); returnR; }}
Now that you have finished creating uniform exception handling in spring boot, the actual implementation is still dependent on spring MVC annotations, and more in-depth use can refer to the documentation for spring MVC.
Unified exception handling for Web applications in Spring boot