SpringMVC异常处理知识点总结
- ResultCode
- CommonCode
- UserCode
- CustomException
- ExceptionCatch
org.springframework.boot spring-boot-starter-parent 2.1.9.RELEASE org.springframework.boot spring-boot-starter-web
统一异常处理
@ControllerAdvice
@ExceptionHandler
@ResponseBody
packagecom.mozq.mybatisplus.mybatisplus01.exception; importcom.mozq.mybatisplus.mybatisplus01.model.CommonCode; importcom.mozq.mybatisplus.mybatisplus01.model.Result; importorg.springframework.web.bind.annotation.ControllerAdvice; importorg.springframework.web.bind.annotation.ExceptionHandler; importorg.springframework.web.bind.annotation.ResponseBody; @ControllerAdvice publicclassExceptionCatch{ @ExceptionHandler(Exception.class) @ResponseBody publicResulthandleException(Exceptione){ returnResult.of(CommonCode.SERVER_ERROR); } @ExceptionHandler(CustomException.class) @ResponseBody publicResulthandleException(CustomExceptione){ returnResult.of(e.getResultCode()); } }
packagecom.mozq.mybatisplus.mybatisplus01.exception; importcom.mozq.mybatisplus.mybatisplus01.model.ResultCode; publicclassCustomExceptionextendsRuntimeException{ privateResultCoderesultCode; publicCustomException(ResultCoderesultCode){ super("错误码:"+resultCode.code()+"错误消息:"+resultCode.message()); this.resultCode=resultCode; } publicResultCodegetResultCode(){ returnresultCode; } }
异常处理测试
@RestController @RequestMapping("/demo") publicclassDemoController{ //单个参数非空校验,使用@RequestParam @RequestMapping("/get") publicStringgetName(@RequestParamStringname){ returnname; } /* publicResultmyCustomException(booleanflag) 不传参数默认false 0-false1-true其他数字抛异常MethodArgumentTypeMismatchException 正确方式: localhost:8080/demo/myCustomException?flag=1 localhost:8080/demo/myCustomException?flag=true */ @RequestMapping("/myCustomException") publicResultmyCustomException(booleanflag){ if(flag){ thrownewCustomException(UserCode.USER_NOT_EXIST); } returnResult.ok(); } }
统一响应结果
packagecom.mozq.mybatisplus.mybatisplus01.model; importlombok.Data; importjava.util.LinkedHashMap; @Data publicclassResultextendsLinkedHashMap{ privatestaticfinalStringSUCCESS="success"; privatestaticfinalStringCODE="code"; privatestaticfinalStringMESSAGE="message"; privatestaticfinalStringRESULT="result"; publicstaticResultof(ResultCoderesultCode){ ResultR=newResult(); R.put(SUCCESS,resultCode.success()); R.put(CODE,resultCode.code()); R.put(MESSAGE,resultCode.message()); returnR; } publicstaticResultok(){ returnResult.of(CommonCode.SUCCESS); } publicstaticResultokWithResult(Objectdata){ ResultR=Result.of(CommonCode.SUCCESS); R.put(RESULT,data); returnR; } publicstaticResultfail(){ returnResult.of(CommonCode.FAIL); } }
响应状态码
packagecom.mozq.mybatisplus.mybatisplus01.model; publicinterfaceResultCode{ booleansuccess(); Stringcode(); Stringmessage(); }
packagecom.mozq.mybatisplus.mybatisplus01.model; publicenumCommonCodeimplementsResultCode{ SUCCESS(true,"000000","处理成功"), FAIL(true,"000001","处理失败"), INVALID_PARAM(false,"100000","无效的参数"), SERVER_ERROR(false,"999999","系统忙,请稍后重试"); privatebooleansuccess; privateStringcode; privateStringmessage; CommonCode(booleansuccess,Stringcode,Stringmessage){ this.success=success; this.code=code; this.message=message; } @Override publicbooleansuccess(){ returnsuccess; } @Override publicStringcode(){ returncode; } @Override publicStringmessage(){ returnmessage; } }
packagecom.mozq.mybatisplus.mybatisplus01.model; publicenumUserCodeimplementsResultCode{ USER_USERNAME_ALREADY_EXIST(false,"200000","用户名已经存在"), USER_TELEPHONE_ALREADY_USED(false,"200001","电话号码已经存在"), USER_NOT_EXIST(false,"200002","用户不存在"); privatebooleansuccess; privateStringcode; privateStringmessage; UserCode(booleansuccess,Stringcode,Stringmessage){ this.success=success; this.code=code; this.message=message; } @Override publicbooleansuccess(){ returnsuccess; } @Override publicStringcode(){ returncode; } @Override publicStringmessage(){ returnmessage; } }
以上就是本次介绍的关于SpringMVC异常处理的相关知识点,感谢大家的学习和对毛票票的支持。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。