@valid和自定义异常

问题的产生:

当有很多参数需要校验时,比如name,age,email等很多参数都需要判空,或者有长度限制时,如果后端写很多if-else就有很多代码,不美观,不优雅.前端每个参数都效验的话工作量也很大

本文旨在解决这个问题,本文使用@valid 注解来解决这个问题.

首先定义一个

统一结果返回

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor; @NoArgsConstructor
@AllArgsConstructor
@Data
public class Result<T> { private String msg;
private Integer code;
private T data;
private static final Integer successCode = 200;
private static Integer errorCode = 500;
private static final String successMsg = "成功";
private static final Object resultNoData = null; public static Result successNoResult() {
return new Result(successMsg, successCode, resultNoData);
} public static <T> Result<T> successWithResult(T data) {
return new Result(successMsg, successCode, data);
} public static Result successWithCodeAndMsg(Integer code, String msg) {
return new Result(msg, code, resultNoData);
} public static Result errorNoResult(String msg) {
return new Result(msg, errorCode, resultNoData);
} public static Result errorWithResult(String msg, Object data) {
return new Result(msg, errorCode, data);
} public static Result errorWithCodeAndMsg(Integer code, String msg) {
return new Result(msg, code, resultNoData);
} }

@valid 注解简单使用

先看下简单使用,复杂的自己查api吧

首先在控制层的参数上加上该注解

import javax.validation.Valid;
import java.util.*; @RestController
@RequestMapping("/test")
public class Test2 { @RequestMapping("test2")
public Result<User> test2(@Valid User user){
return Result.successWithResult(user);
}
}

然后在实体类中加上如下注解

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size; public class User { /**
* @NotEmpty:不能为null,而且长度必须大于0
* @NotBlank:只用在String上,表示传进来的值不能为null,而且调用trim()后,长度必须大于0
* @NotNull:不能为null,但可以为empty
* @Length(message = "名称不能超过个 {max} 字符", max = 10)
* @Range(message = "年龄范围为 {min} 到 {max} 之间", min = 1, max = 100)
*/
@NotNull( message = "ID不能为空")
private Integer id; @NotBlank( message = "昵称不能为空")
@Size( min = 2,max = 5,message ="昵称的字数个数必须在0和5之间" )
private String name; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
}

请求该接口

http://localhost:8080/test/test2?id=5&name=xxwwwww

查看效果,如下

可以看到,已经能表示该请求参数错误

但是还有个问题,我们不能直接这样子直接返回给前端,需要返回统一的结果

全局异常处理

Spring-boot对于异常的处理也做了不错的支持,

它提供了一个 @ControllerAdvice注解以及 @ExceptionHandler注解,

前者是用来开启全局的异常捕获,后者则是说明捕获哪些异常,对那些异常进行处理。如下

自定义异常

import lombok.Data;

@Data
public class DefinitionException extends RuntimeException {
private Integer errorCode;
private String errorMsg; public DefinitionException(){ }
public DefinitionException(Integer errorCode, String errorMsg) {
this.errorCode = errorCode;
this.errorMsg = errorMsg;
} }

异常处理

@ControllerAdvice
public class GlobalExceptionHandler { /**
* 处理自定义异常
*/
@ExceptionHandler(value = DefinitionException.class)
@ResponseBody
public Result bizExceptionHandler(DefinitionException definitionException) {
Result result=new Result();
result.setCode(definitionException.getErrorCode());
result.setMsg(definitionException.getErrorMsg());
return result;
} /**
* 处理异常
*/
@ExceptionHandler(value = Exception.class)
@ResponseBody
public Result runtimeExceptionHandler(Exception exception) {
Result result=new Result();
result.setCode(500);
result.setMsg(exception.getMessage());
return result;
}
}

测试代码

@RequestMapping("test3")
public Result<String> test3(){
int i=1/0;
return Result.successWithResult("test3");
} @RequestMapping("test4")
public Result<String> test4(){
throw new DefinitionException(500,"啊哦,报错了");
}

查看结果

将@valid注解抛的异常也返回统一格式

我们再请求一下这个接口

http://localhost:8080/test/test2?id=5&name=xxwwwww

看下结果

返回格式是统一的格式了,但是返回的信息不太友好,我们看看怎么优化

debug一下,看看这个是什么异常

我们看到,这个异常是org.springframework.validation.BindException类的,

我们看下这个类的具体内容,我们只要我们想要的信息就行

这里,我们只要这个信息就可以了我们改动后如下

    /**
* 处理异常
*/
@ExceptionHandler(value = Exception.class)
@ResponseBody
public Result runtimeExceptionHandler(Exception exception) {
Result result=new Result();
if(exception instanceof BindException){//注解类异常
StringBuilder sb = new StringBuilder();
BindException bindException= (BindException) exception;
BindingResult bindingResult = bindException.getBindingResult();
List<ObjectError> allErrors = bindingResult.getAllErrors();
for (ObjectError item : allErrors) {
sb
.append(item.getDefaultMessage())
.append(',');
}
sb.deleteCharAt(sb.length()-1);
result.setCode(500);
result.setMsg(sb.toString()); }
return result;
}

再请求该接口,得到结果

到此为止,我们已经得到了我们想要的结果

优化代码

最后,我们在优化一下全局异常处理代码如下

import com.yoocar.util.Result;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List; @ControllerAdvice
public class GlobalExceptionHandler { /**
* 处理自定义异常
*/
@ExceptionHandler(value = Exception.class)
@ResponseBody
public Result exceptionHandler(Exception exception) {
Result result=new Result();
//自定义类型异常
if(exception instanceof DefinitionException){
DefinitionException definitionException= (DefinitionException) exception;
result.setCode(definitionException.getErrorCode());
result.setMsg(definitionException.getErrorMsg());
}else if(exception instanceof BindException){//@valid注解抛出的异常
//使用StringBuilder来拼接错误信息,减少对象开销
StringBuilder stringBuilder = new StringBuilder();
//获取并拼接所有错误信息
BindException bindException= (BindException) exception;
BindingResult bindingResult = bindException.getBindingResult();
List<ObjectError> allErrors = bindingResult.getAllErrors();
for (ObjectError item : allErrors) {
stringBuilder.append(item.getDefaultMessage())
.append(',');
}
//删除最后一个逗号
stringBuilder.deleteCharAt(stringBuilder.length()-1);
result.setCode(600);//这里自定义了600用于表示参数有误
result.setMsg(stringBuilder.toString());
}else {//其他异常
result.setCode(500);
result.setMsg(exception.getMessage());
}
return result;
} }

至此,本文结束.文章中若有错误和疏漏之处,还请各位大佬不吝指出,谢谢大家!

@valid和自定义异常的更多相关文章

  1. @Valid 数据校验 + 自定义全局异常信息

    关于javax.validation.Validator校验的使用 对于要校验的实体类:其需要校验的字段上需要添加注解 实际例子 使用:首先要拿到 validator的子类 Validator val ...

  2. 【springboot】@Valid参数校验

    转自: https://blog.csdn.net/cp026la/article/details/86495659 扯淡: 刚开始写代码的时候对参数的校验要么不做.要么写很多类似 if( xx == ...

  3. 小白解决CENTOS7错误:Cannot find a valid baseurl for repo: base/7/x86_6

    刚入手的MacBook想着学点东西,本汪还是决定玩玩CentOS服务器,安装好了VirtualBox + CentOS. 打开一看,懵逼了!命令行! 行吧,先装个图形界面: $sudo yum gro ...

  4. 记一个mvn奇怪错误: Archive for required library: 'D:/mvn/repos/junit/junit/3.8.1/junit-3.8.1.jar' in project 'xxx' cannot be read or is not a valid ZIP file

    我的maven 项目有一个红色感叹号, 而且Problems 存在 errors : Description Resource Path Location Type Archive for requi ...

  5. Leetcode 笔记 35 - Valid Soduko

    题目链接:Valid Sudoku | LeetCode OJ Determine if a Sudoku is valid, according to: Sudoku Puzzles - The R ...

  6. 安装CentOS7文字界面版后,无法联网,用yum安装软件提示 cannot find a valid baseurl for repo:base/7/x86_64 的解决方法

    *无法联网的明显表现会有: 1.yum install出现 Error: cannot find a valid baseurl or repo:base 2.ping host会提示unknown ...

  7. [LeetCode] Valid Word Square 验证单词平方

    Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...

  8. [LeetCode] Valid Word Abbreviation 验证单词缩写

    Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...

  9. [LeetCode] Graph Valid Tree 图验证树

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

随机推荐

  1. JS常用数值验证

    1.正整数验证 正整数是大于0的整数. function validateInteger(val) { return Number.isInteger(val) && val > ...

  2. OSS对象储存

    简介 阿里云对象存储服务(Object Storage Service,简称 OSS),是阿里云提供的海量.安全.低成本.高可靠的云存储服务. 使用流程 名词解释  Endpoint(访问域名) Ac ...

  3. 后端程序员之路 10、gbdt(Gradient Boosting Decision Tree)

    1.GbdtModelGNode,含fea_idx.val.left.right.missing(指向left或right之一,本身不分配空间)load,从model文件加载模型,xgboost输出的 ...

  4. ng-class动态类几种用法

    方法1.逻辑在后面的中括号里面 ng-class="{true : 'checker disabled',false : 'checker' }[selectAllButton]" ...

  5. 一个 java 文件的执行过程详解

    平时我们都使用 idea.eclipse 等软件来编写代码,在编写完之后直接点击运行就可以启动程序了,那么这个过程是怎么样的? 总体过程 我们编写的 java 文件在由编译器编译后会生成对应的 cla ...

  6. CCF(公共钥匙盒):思维+模拟

    公共钥匙盒 201709-2 这题的思路一开始不是很清晰,一开始想用贪心去做.但是发现按照题目的思路不对.所以这里采用的是类似于多项式的加减的处理. #include<iostream> ...

  7. mongoDB服务器连接不上Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException:

    一大早打开node项目就报错,终端报 UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connect ECONNREFU ...

  8. 选择 FreeBSD 而不是 Linux 的技术性原因1

    Ports FreeBSD Ports 是一个惊人的工程壮举.NetBSD 的 pkgsrc (package source) 和 OpenBSD 的 ports collection 都源于 Fre ...

  9. C#开发BIMFACE系列35 服务端API之模型对比6:获取模型构建对比分类树

    系列目录     [已更新最新开发文章,点击查看详细] BIMFACE平台提供了服务端"获取模型对比构件分类树"API.目录树返回结果以树状层级关系显示了增删改的构件信息,里面无法 ...

  10. ajax轮询原理及其实现方式

    ajax轮询原理及其实现方式 ajax轮询的两种方式 方式1:设定一个定时器,无论有无结果返回,时间一到就会继续发起请求,这种轮询耗费资源,也不一定能得到想要的数据,这样的轮询是不推荐的. 方式2: ...