一.前言html
基於上一篇 springBoot2.0 配置shiro實現權限管理
java
這一篇配置 異常統一處理web
二.新建文件夾:common,paramredis
三.返回結果集對象spring
1.ResultData.javamybatis
package com.example.demo2.common; import lombok.Getter; import lombok.Setter; import lombok.ToString; import java.util.HashMap; import java.util.Map; /** * @author sssr * @version 1.0 * @Description: 統一返回結果集對象 * @date 2019/2/20 */ @Getter @Setter @ToString public class ResultData { private boolean ret; private String msg; private Object data; public ResultData() { } public ResultData(boolean ret){ this.ret =ret; } public static ResultData success(Object data, String msg){ ResultData resultData = new ResultData(true); resultData.data = data; resultData.msg = msg; return resultData; } public static ResultData success(Object data){ ResultData resultData = new ResultData(true); resultData.data = data; return resultData; } public static ResultData success(){ return new ResultData(true); } public static ResultData fail(String msg){ ResultData resultData = new ResultData(false); resultData.msg = msg; return resultData; } public Map<String,Object> toMap(){ HashMap<String,Object> result = new HashMap(); result.put("ret",ret); result.put("msg",msg); result.put("data",data); return result; } }
四.自定義全局異常類app
1.GlobalException.java測試
package com.example.demo2.common; /** * @author sssr * @version 1.0 * @Description: 自定義異常類 * @date 2019/2/20 */ public class GlobalException extends RuntimeException{ public GlobalException() { } public GlobalException(String message) { super(message); } public GlobalException(String message, Throwable cause) { super(message, cause); } public GlobalException(Throwable cause) { super(cause); } public GlobalException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } }
五.異常捕獲處理類this
1.GlobalExceptionResolver.javaspa
package com.example.demo2.common; import org.springframework.validation.BindException; import org.springframework.validation.FieldError; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestController; import java.io.UnsupportedEncodingException; import java.util.List; /** * @author sssr * @version 1.0 * @Description: 異常捕獲處理類 * @date 2019/2/20 */ @ControllerAdvice @RestController public class GlobalExceptionResolver { /** * 全局異常處理 * @param e * @return */ @ExceptionHandler(Exception.class) public ResultData exception(Exception e){ return ResultData.fail(e.getMessage()); } /** * 自定義異常處理 * @param e * @return */ @ExceptionHandler(GlobalException.class) public ResultData globalException(Exception e){ return ResultData.fail(e.getMessage()); } /** * lombok請求參數不合法異常處理 * @param e * @return * @throws UnsupportedEncodingException */ @ExceptionHandler(BindException.class) public ResultData bindException(BindException e) throws UnsupportedEncodingException { e.printStackTrace(); List<FieldError> fieldErrors = e.getFieldErrors(); String errorMsg="{"; for (FieldError fieldError:fieldErrors){ errorMsg+=fieldError.getField()+":"+fieldError.getDefaultMessage()+","; } if(errorMsg.length()>1){ errorMsg = errorMsg.substring(0,errorMsg.length()-1); } errorMsg+="}"; return ResultData.fail(errorMsg); } }
六.測試參數接收類
1.TestParam.java
package com.example.demo2.param; import lombok.Getter; import lombok.Setter; import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotEmpty; import javax.validation.constraints.NotNull; import java.util.List; /** * @author sssr * @version 1.0 * @Description: * @date 2019/2/20 */ @Getter @Setter public class TestParam { @NotBlank private String msg; @NotNull private Integer id; @NotEmpty private List<String> list; }
七.測試控制器
1.TestController.java
package com.example.demo2.controller; import com.example.demo2.common.GlobalException; import com.example.demo2.param.TestParam; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.validation.Valid; /** * @author sssr * @version 1.0 * @Description: 測試控制器 * @date 2019/2/20 */ @RestController @RequestMapping("/test") public class TestController { @RequestMapping("/exception") public String exception() throws Exception { throw new Exception("全局錯誤"); } @RequestMapping("/globalException") public String globalException() throws GlobalException { throw new GlobalException("自定義錯誤"); } @RequestMapping("/bindException") public String bindException(@Valid TestParam testParam) { return testParam.getMsg(); } }
八.測試效果