Spring Boot Exception processing method: @ControllerAdvice annotation Usage

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.