1. Create exception handling packages and classes first
2, using @controlleradvice annotations, the global catch Exception class, as long as the action on the @requestmapping, all the exceptions will be captured
Package Com.example.demo.exception;import Org.springframework.web.bind.annotation.controlleradvice;import Org.springframework.web.bind.annotation.exceptionhandler;import Org.springframework.web.bind.annotation.responsebody;import Java.util.hashmap;import Java.util.Map;/** * Global catch Exception class, as long as the action on @requestmapping, all the exceptions will be captured*/@ResponseBody @controlleradvice Public classMycontrolleradvice {@ExceptionHandler (value= Exception.class) PublicMap<string,object>Errorhandle (Exception e) {Map<String,Object> map =NewHashmap<string,object>(); Map.put ("Code",-1); Map.put ("msg", E.getmessage ()); returnmap; }}
There is a need to pay attention to this is to add @ResponseBody annotations, if not add, we try, error:
javax.servlet.ServletException: Circular view path [Hello]: Would dispatch back to the current handler URL [/hello] again.
Check your Viewresolver setup! (Hint: This could be the result of a unspecified view, due to default View name Generation.) At Org.springframework.web.servlet.view.InternalResourceView.prepareForRendering (Internalresourceview.java: 209) ~[spring-webmvc-5.0.6. Release.jar:5.0.6. RELEASE] at Org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel ( Internalresourceview.java:147) ~[spring-webmvc-5.0.6. Release.jar:5.0.6. RELEASE] at Org.springframework.web.servlet.view.AbstractView.render (Abstractview.java:314) ~[spring-webmvc-5.0.6. Release.jar:5.0.6. RELEASE] at Org.springframework.web.servlet.DispatcherServlet.render (Dispatcherservlet.java:1325) ~[spring-webmvc-5.0.6. Release.jar:5.0.6. RELEASE] at Org.springframework.web.servlet.DispatcherServlet.processDispatchResult (Dispatcherservlet.java:1069) ~[spring-webmvc-5.0.6. Release.jar:5.0.6. RELEASE] at Org.springframework.web.servlet.DispatcherServlet.doDispatch (Dispatcherservlet.java:1008) ~[spring-webmvc-5.0.6. Release.jar:5.0.6. RELEASE] at Org.springframework.web.servlet.DispatcherServlet.doService (Dispatcherservlet.java:925) ~[spring-webmvc-5.0.6. Release.jar:5.0.6. RELEASE] at Org.springframework.web.servlet.FrameworkServlet.processRequest (Frameworkservlet.java:974) ~[spring-webmvc-5.0.6. Release.jar:5.0.6. RELEASE] at Org.springframework.web.servlet.FrameworkServlet.doGet (Frameworkservlet.java:866) ~[spring-webmvc-5.0.6. Release.jar:5.0.6. RELEASE] at Javax.servlet.http.HttpServlet.service (Httpservlet.java:635) ~[tomcat-embed-core-8.5. to. Jar:8.5. to] at Org.springframework.web.servlet.FrameworkServlet.service (Frameworkservlet.java:851) ~[spring-webmvc-5.0.6. Release.jar:5.0.6. RELEASE] at Javax.servlet.http.HttpServlet.service (Httpservlet.java:742) ~[tomcat-embed-core-8.5. to. Jar:8.5. to] at Org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (Applicationfilterchain.java:231) ~[tomcat-embed-core-8.5. to. Jar:8.5. to]
Because it's a JSON format, you have to have @ResponseBody
3. Test: Create an exception in Hello
Package Com.example.demo;import Org.springframework.beans.factory.annotation.value;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.RestController; @RestController Public classHellocontroller {@Value ("${gwf.name}") PrivateString msg; @RequestMapping ("/hello") PublicString Hello () {intnum =1/0; return This. msg; }}
Results:
Spring Boot Exception processing method: @ControllerAdvice annotation Usage