您好,登錄后才能下訂單哦!
一、單個controller范圍的異常處理
package com.xxx.secondboot.web; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.xxx.secondboot.exception.MyExceptionResponse; import io.swagger.annotations.Api; @Api("測試controllerAdvice和全局異常處理") @RestController @RequestMapping("/advice1") public class AdviceController { @RequestMapping(value = "/test1", method = RequestMethod.GET) public String test1() { throw new RuntimeException("advice1 - exception1"); } @RequestMapping(value = "/test2", method = RequestMethod.GET) public String test2() { throw new RuntimeException("advice1 - exception2"); } @ExceptionHandler(RuntimeException.class) public MyExceptionResponse exceptionHandler() { MyExceptionResponse resp = new MyExceptionResponse(); resp.setCode(300); resp.setMsg("exception-Handler"); return resp; } }
說明:
二、全部controller范圍內起作用的異常處理(全局異常處理)
1、全局異常處理類
package com.xxx.secondboot.web; import javax.servlet.http.HttpServletResponse; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import com.xxx.secondboot.exception.MyExceptionResponse; import com.xxx.secondboot.exception.MyRuntimeException; //@ControllerAdvice(annotations=RestController.class) //@ControllerAdvice(basePackages={"com.xxx","com.ooo"}) @ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(RuntimeException.class) // @ExceptionHandler(value={RuntimeException.class,MyRuntimeException.class}) // @ExceptionHandler//處理所有異常 @ResponseBody //在返回自定義相應類的情況下必須有,這是@ControllerAdvice注解的規定 public MyExceptionResponse exceptionHandler(RuntimeException e, HttpServletResponse response) { MyExceptionResponse resp = new MyExceptionResponse(); resp.setCode(300); resp.setMsg("exception-Handler"); // response.setStatus(600); return resp; } }
說明:
2、controller
package com.xxx.secondboot.web; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import io.swagger.annotations.Api; @Api("測試controllerAdvice和全局異常處理") @RestController @RequestMapping("/advice1") public class AdviceController { @RequestMapping(value = "/test1", method = RequestMethod.GET) public String test1() { throw new RuntimeException("advice1 - exception1"); } @RequestMapping(value = "/test2", method = RequestMethod.GET) public String test2() { throw new RuntimeException("advice1 - exception2"); } // @ExceptionHandler(RuntimeException.class) // public MyExceptionResponse exceptionHandler() { // MyExceptionResponse resp = new MyExceptionResponse(); // resp.setCode(300); // resp.setMsg("exception-Handler"); // return resp; // } }
注意:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。