亲宝软件园·资讯

展开

SpringMVC异常处理知识点总结

人气:0
<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;
  }
}

以上就是本次介绍的关于SpringMVC异常处理的相关知识点,感谢大家的学习和对的支持。

您可能感兴趣的文章:

加载全部内容

相关教程
猜你喜欢
用户评论