自定義異常,在平常項目開發中用處很大html
一、先建立一個異常統一處理類 java
package com.black.example.helloworld.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; /** * 異常統一處理方法 */ @ControllerAdvice public class MyHandlerAdvice { /** * 全局捕獲異常,只要做用在@RequestMapping方法上,全部的信息都會被捕捉到 * @param ex * @return */ @ResponseBody @ExceptionHandler(value = Exception.class) public Map<String,Object> errorHandler(Exception ex){ Map<String,Object> map = new HashMap<String,Object>(); map.put("code",-1); map.put("msg",ex.getMessage()); return map; } }
二、假定在HelloWorldHandler中的index方法上,設置一些異常代碼,以下圖:web
@RequestMapping(value = "/index.html") public String index(){ int code = 1/0; return "Hello Black!!!!!"; }
三、重啓訪問 http://localhost:8090/black/index.htmlspring
一、自定義異常實體類app
package com.black.example.helloworld.exception; /** * 自定義異常 */ public class BusinessException extends RuntimeException { private String code; private String msg; public BusinessException(String code,String msg){ this.code = code; this.msg = msg; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } }
二、自定義異常方法this
package com.black.example.helloworld.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; /** * 異常統一處理方法 */ @ControllerAdvice public class MyHandlerAdvice { /** * 全局捕獲異常,只要做用在@RequestMapping方法上,全部的信息都會被捕捉到 * @param ex * @return */ @ResponseBody @ExceptionHandler(value = Exception.class) public Map<String,Object> errorHandler(Exception ex){ Map<String,Object> map = new HashMap<String,Object>(); map.put("code",-1); map.put("msg",ex.getMessage()); return map; } /** * 自定義異常 * @param ex * @return */ @ResponseBody @ExceptionHandler(value = BusinessException.class) public Map<String,Object> errorBussinessHandler(BusinessException ex){ Map<String,Object> map = new HashMap<String,Object>(); map.put("code",ex.getCode()); map.put("msg",ex.getMsg()); return map; } }
三、假定程序拋出異常spa
package com.black.example.helloworld.web; import com.black.example.helloworld.exception.BusinessException; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * Created by 10250H on 2018/5/15. */ @RestController public class HelloWorldHandler { //獲取application.properties自定義配置的值 @Value("${black.msg}") private String msg; @RequestMapping(value = "/index.html") public String index(){ /*int code = 1/0; return "Hello Black!!!!!";*/ throw new BusinessException("500","用戶名或者密碼錯誤"); } @RequestMapping(value = "/customConfig.html") public String customConfig(){ return this.msg; } }
四、重啓服務,觀察結果code