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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。