Spring Boot 全局異常配置
阿新 • • 發佈:2018-06-13
處理異常 framework details TE exce model PE exc urn
Spring Boot 全局異常配置,處理異常控制器需要和發生異常的方法在一個類中。使用 ControllerAdvice 註解
package com.li.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler;import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class ExceptionHandlerController { @RequestMapping("/exceptionTest") @ResponseBody public String test() throws Exception{ System.out.println("測試"); int i=1/0; return Integer.toString(i); } @ControllerAdvice class HandlerException{ @ExceptionHandler(Exception.class) @ResponseBody public String defultExcepitonHandler(Exception e) { // return "{\"error\":\"error\"}"; return"error"; } } }
https://blog.csdn.net/qq_34083066/article/details/79424142
Spring Boot 全局異常配置