Spring Boot统一异常处理以及参数校验

栏目: Java · 发布时间: 7年前

内容简介:一般情况我们前端向后端传递参数都是2种方式因此本文分别讲述Api设计如下:

一般情况我们前端向后端传递参数都是2种方式 JSON 或者 表单提交

因此本文分别讲述 JSON提交 参数校验和 表单提交 参数校验在Spring Boot中是如何操作,以及校验失败如何统一转交给异常处理类去处理的。

Api设计如下:

表单方式 : http://localhost:8080/get-args-valid?username=xxx&password=xxx

JSON方式 : http://localhost:8080/post-args-valid

{
    "username":"123",
    "password":"123"
}

复制代码

采用JSON方式提交,所以设置 content-type 如下:

Content-Type : application/json

新建一个Spring Boot项目

Api如下设计:

/**
 * @author: hujiansong
 * @email: 1358199510@qq.com
 * @since: 2019/1/29 16:53
 */
@RestController
public class ValidController {

    @GetMapping("/get-args-valid")
    public String getArgsValid(String username, String password) {
        return null;
    }

    @PostMapping("/post-args-valid")
    public String postArgsValid(@RequestBody User user) {
        return null;
    }

    @Data
    class User {
        String username;

        String password;
    }
}

复制代码

先讲 JSON 方式如何进行参数校验

JSON方式 :

@RestController
public class ValidController {


    @PostMapping("/post-args-valid")
    public String postArgsValid(@Valid<1> @RequestBody User user) {
        return null;
    }

    @Data
    static class User {
        @NotNull(message = "用户名不能为空")<2>
        String username;

        @NotNull(message = "密码不能为空")
        String password;
    }
}

复制代码

注意: 这里内部类 User 需要加上static,否则json传过来无法解析

<1>: @Valid 表示这个实体参数交给Spring去校验

<2>: @NotNull 校验规则

如上2步操作就可以完成参数校验:

Spring Boot统一异常处理以及参数校验

可以看到如何 password 不传递,spring 已经帮我们做了参数校验,再来看看 表单方式

表单方式 :

@RestController
@Validated<1>
public class ValidController {

    @GetMapping("/get-args-valid")
    public String getArgsValid(@NotNull(message = "用户名不能空")<2> String username, @NotNull(message = "密码不能为空") String password) {
        return null;
    }

}

复制代码

同样也是2步搞定

<1>: @Validated ,交给Spring去校验

<2>: @NotNull 校验规则

看看如果 password 不传递会返回什么:

Spring Boot统一异常处理以及参数校验

可见,Spring已经替我们做了参数校验

Spring 还包含了很多校验规则如下:

注解 解释
@Null 被注释的元素必须为 null
@NotNull 被注释的元素必须不为 null
@AssertTrue 被注释的元素必须为 true
@AssertFalse 被注释的元素必须为 false
@Min(value) 被注释的元素必须是一个数字,其值必须大于等于指定的最小值
@Max(value) 被注释的元素必须是一个数字,其值必须小于等于指定的最大值
@DecimalMin 被注释的元素必须是一个数字,其值必须大于等于指定的最小值
@DecimalMax 被注释的元素必须是一个数字,其值必须小于等于指定的最大值
@Size(max=, min=) 被注释的元素的大小必须在指定的范围内
@Digits 被注释的元素必须是一个数字,其值必须在可接受的范围内
@Past 被注释的元素必须是一个过去的日期
@Future 被注释的元素必须是一个将来的日期
@Pattern(regex=) 被注释的元素必须符合指定的正则表达式

异常统一处理

上面介绍了如何让Spring校验我们的参数,那么可以看到 JSON 方式校验返回的结果一大串,不是十分优雅。那么利用统一异常处理则可优雅返回参数校验结果。

JSON方式 :校验失败后,会抛出一个 MethodArgumentNotValidException

表单方式 :校验失败,会抛出一个 ConstraintViolationException

因此只需要在统一异常处理类里面处理这2个异常即可。

ExceptionHanlder

表单方式 :

@RestControllerAdvice
public class ExceptionHandler {

    @org.springframework.web.bind.annotation.ExceptionHandler(ConstraintViolationException.class)
    public Map<String, Object> methodArgNotValidException(ConstraintViolationException cve, HttpServletRequest httpServletRequest) {
        Set<ConstraintViolation<?>> cves = cve.getConstraintViolations();
        StringBuffer errorMsg = new StringBuffer();
        cves.forEach(ex -> errorMsg.append(ex.getMessage()));
        Map<String, Object> respMap = new HashMap<>(4);
        respMap.put("code", -1);
        respMap.put("msg", errorMsg);
        return respMap;
    }
}
复制代码

重新调用:

Spring Boot统一异常处理以及参数校验

JSON方式 :

@RestControllerAdvice
public class ExceptionHandler {

    @org.springframework.web.bind.annotation.ExceptionHandler({MethodArgumentNotValidException.class})
    public Map<String, Object> methodDtoNotValidException(Exception ex, HttpServletRequest request) {
        MethodArgumentNotValidException c = (MethodArgumentNotValidException) ex;
        List<ObjectError> errors = c.getBindingResult().getAllErrors();
        StringBuffer errorMsg = new StringBuffer();
        errors.stream().forEach(x -> {

            errorMsg.append(x.getDefaultMessage()).append(";");
        });
        Map<String, Object> respMap = new HashMap<>(4);
        respMap.put("code", -1);
        respMap.put("msg", errorMsg);
        return respMap;
    }

}
复制代码

同样调用,这次 username 为空试试看:

Spring Boot统一异常处理以及参数校验

完整的代码

Spring Boot统一异常处理以及参数校验

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

An Introduction to the Analysis of Algorithms

An Introduction to the Analysis of Algorithms

Robert Sedgewick、Philippe Flajolet / Addison-Wesley Professional / 1995-12-10 / CAD 67.99

This book is a thorough overview of the primary techniques and models used in the mathematical analysis of algorithms. The first half of the book draws upon classical mathematical material from discre......一起来看看 《An Introduction to the Analysis of Algorithms》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具