SpringMVC异常处理知识点总结

网友投稿 379 2023-07-17

SpringMVC异常处理知识点总结

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

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", "无效的参数"),

SERVEhttp://R_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

uEscMpH public String message() {

return message;

}

}

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

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:spring boot发简单文本邮件案例
下一篇:spring boot过滤器FilterRegistrationBean实现方式
相关文章

 发表评论

暂时没有评论,来抢沙发吧~