SpringMVC中怎么對異常進行處理,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
十載專注建站、設(shè)計、互聯(lián)網(wǎng)產(chǎn)品按需求定制設(shè)計服務(wù),業(yè)務(wù)涵蓋成都品牌網(wǎng)站建設(shè)、商城網(wǎng)站開發(fā)、重慶小程序開發(fā)公司、軟件系統(tǒng)開發(fā)、重慶APP開發(fā)公司等。憑借多年豐富的經(jīng)驗,我們會仔細了解每個客戶的需求而做出多方面的分析、設(shè)計、整合,為客戶設(shè)計出具風(fēng)格及創(chuàng)意性的商業(yè)解決方案,創(chuàng)新互聯(lián)公司更提供一系列網(wǎng)站制作和網(wǎng)站推廣的服務(wù),以推動各中小企業(yè)全面信息數(shù)字化,并利用創(chuàng)新技術(shù)幫助各行業(yè)提升企業(yè)形象和運營效率。
ResultCode
CommonCode
UserCode
CustomException
ExceptionCatch
org.springframework.boot spring-boot-starter-parent 2.1.9.RELEASE org.springframework.boot spring-boot-starter-web
統(tǒng)一異常處理
@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 { //單個參數(shù)非空校驗,使用@RequestParam @RequestMapping("/get") public String getName(@RequestParam String name){ return name; } /* public Result myCustomException(boolean flag) 不傳參數(shù)默認false 0-false 1-true 其他數(shù)字拋異常 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(); } }
統(tǒng)一響應(yīng)結(jié)果
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); } }
響應(yīng)狀態(tài)碼
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", "無效的參數(shù)"), SERVER_ERROR(false, "999999", "系統(tǒng)忙,請稍后重試"); 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", "用戶名已經(jīng)存在"), USER_TELEPHONE_ALREADY_USED(false, "200001", "電話號碼已經(jīng)存在"), 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; } }
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。