SpringMVC异常处理知识点总结

时间:2021-05-19

  • 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;@ControllerAdvicepublic 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; } @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;@Datapublic 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异常处理的相关知识点,感谢大家的学习和对的支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章