SpringMVC中怎么对异常进行处理
SpringMVC中怎么对异常进行处理,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
十载专注建站、设计、互联网产品按需求定制设计服务,业务涵盖成都品牌网站建设、商城网站开发、重庆小程序开发公司、软件系统开发、重庆APP开发公司等。凭借多年丰富的经验,我们会仔细了解每个客户的需求而做出多方面的分析、设计、整合,为客户设计出具风格及创意性的商业解决方案,创新互联公司更提供一系列网站制作和网站推广的服务,以推动各中小企业全面信息数字化,并利用创新技术帮助各行业提升企业形象和运营效率。
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
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; } }
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。
文章标题:SpringMVC中怎么对异常进行处理
网页URL:http://cdiso.cn/article/jihipg.html