您好,登錄后才能下訂單哦!
SpringMVC中怎么對異常進行處理,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
ResultCode
CommonCode
UserCode
CustomException
ExceptionCatch
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
統一異常處理
@ControllerAdvice
@ExceptionHandler
@ResponseBody
package com.mozq.mybatisplus.mybatisplus01.exception; import com.mozq.mybatisplus.mybatisplus01.model.CommonCode; import com.mozq.mybatisplus.mybatisplus01.model.Result; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; @ControllerAdvice public class ExceptionCatch { @ExceptionHandler(Exception.class) @ResponseBody public Result handleException(Exception e){ return Result.of(CommonCode.SERVER_ERROR); } @ExceptionHandler(CustomException.class) @ResponseBody public Result handleException(CustomException e){ return Result.of(e.getResultCode()); } }
package com.mozq.mybatisplus.mybatisplus01.exception; import com.mozq.mybatisplus.mybatisplus01.model.ResultCode; public class CustomException extends RuntimeException { private ResultCode resultCode; public CustomException(ResultCode resultCode){ super("錯誤碼:" + resultCode.code() + "錯誤消息:" + resultCode.message()); this.resultCode = resultCode; } public ResultCode getResultCode(){ return resultCode; } }
異常處理測試
@RestController @RequestMapping("/demo") public class DemoController { //單個參數非空校驗,使用@RequestParam @RequestMapping("/get") public String getName(@RequestParam String name){ return name; } /* public Result myCustomException(boolean flag) 不傳參數默認false 0-false 1-true 其他數字拋異常 MethodArgumentTypeMismatchException 正確方式: localhost:8080/demo/myCustomException?flag=1 localhost:8080/demo/myCustomException?flag=true */ @RequestMapping("/myCustomException") public Result myCustomException(boolean flag){ if(flag){ throw new CustomException(UserCode.USER_NOT_EXIST); } return Result.ok(); } }
統一響應結果
package com.mozq.mybatisplus.mybatisplus01.model; import lombok.Data; import java.util.LinkedHashMap; @Data public class Result extends LinkedHashMap { private static final String SUCCESS = "success"; private static final String CODE = "code"; private static final String MESSAGE = "message"; private static final String RESULT = "result"; public static Result of(ResultCode resultCode){ Result R = new Result(); R.put(SUCCESS, resultCode.success()); R.put(CODE, resultCode.code()); R.put(MESSAGE, resultCode.message()); return R; } public static Result ok(){ return Result.of(CommonCode.SUCCESS); } public static Result okWithResult(Object data){ Result R = Result.of(CommonCode.SUCCESS); R.put(RESULT, data); return R; } public static Result fail(){ return Result.of(CommonCode.FAIL); } }
響應狀態碼
package com.mozq.mybatisplus.mybatisplus01.model; public interface ResultCode { boolean success(); String code(); String message(); }
package com.mozq.mybatisplus.mybatisplus01.model; public enum CommonCode implements ResultCode { SUCCESS(true, "000000", "處理成功"), FAIL(true, "000001", "處理失敗"), INVALID_PARAM(false, "100000", "無效的參數"), SERVER_ERROR(false, "999999", "系統忙,請稍后重試"); private boolean success; private String code; private String message; CommonCode(boolean success, String code, String message){ this.success = success; this.code = code; this.message = message; } @Override public boolean success() { return success; } @Override public String code() { return code; } @Override public String message() { return message; } }
package com.mozq.mybatisplus.mybatisplus01.model; public enum UserCode implements ResultCode { USER_USERNAME_ALREADY_EXIST(false, "200000", "用戶名已經存在"), USER_TELEPHONE_ALREADY_USED(false, "200001", "電話號碼已經存在"), USER_NOT_EXIST(false, "200002", "用戶不存在"); private boolean success; private String code; private String message; UserCode(boolean success, String code, String message){ this.success = success; this.code = code; this.message = message; } @Override public boolean success() { return success; } @Override public String code() { return code; } @Override public String message() { return message; } }
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。