SpringBoot注解@NotNull,@NotBlank,@Valid自动判定空值
一、前言
搭建springboot项目,我们都是采用的Restful接口,那么问题来了,当前端调用接口或者是其他项目调用时,我们不能单一靠调用方来控制参数的准确性,自己也要对一些非空的值进行判定。
注解所用包:
import javax.validation.constraints.*;
二、常用的校验注解
所在包(注意别导错包):import javax.validation.constraints.NotNull;
@Null 被注释的元素必须为null
@NotNull 被注释的元素不能为null
@AssertTrue 被注释的元素必须为true
@AssertFalse 被注释的元素必须为false
@Min(value) 被注释的元素必须是一个数字,其值必须大于等于指定的最小值
@Max(value) 被注释的元素必须是一个数字,其值必须小于等于指定的最大值
@DecimalMin(value) 被注释的元素必须是一个数字,其值必须大于等于指定的最小值
@DecimalMax(value) 被注释的元素必须是一个数字,其值必须小于等于指定的最大值
@Size(max,min) 被注释的元素的大小必须在指定的范围内。
@Digits(integer,fraction) 被注释的元素必须是一个数字,其值必须在可接受的范围内
@Past 被注释的元素必须是一个过去的日期
@Future 被注释的元素必须是一个将来的日期
@Pattern(value) 被注释的元素必须符合指定的正则表达式。
@Email 被注释的元素必须是电子邮件地址
@Length 被注释的字符串的大小必须在指定的范围内
@NotEmpty 被注释的字符串必须非空
@Range 被注释的元素必须在合适的范围内
三、项目实战【别忘记加@Valid
注解】
说明
1、在Bean的字段上增加注解
和相应提示语
就好了。
@NotNull
private String name;
2、在controller上【别忘记@Valid
注解】, @Valid Bean bean,BindingResult result
public ReturnT insert(@Valid LuteArticle luteArticle, BindingResult result) {
if (result.hasErrors()) {
return ReturnT.FAIL(HttpStatus.HTTP_BAD_REQUEST, Objects.requireNonNull(result.getFieldError()).getDefaultMessage());
}
return luteArticleService.insert(luteArticle);
}
作为参数,当name未传入时,可以在result里面获得校验信息
3、在Bean的构造器使用
public Bean(@NonNull Integer id) {
this.id=id;
}
new Bean(null);会报空指针异常
实战 ex:
/**
* 文章标题
*/
@NotNull(message = "标题不能为空")
private String articleTitle;
Model:
package com.nathan.lute.module.pojo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Date;
/**
-
文章表
-
@author Na7han 2019-11-26
*/
@Data
@TableName("lute_article")
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
public class LuteArticle implements Serializable {
private static final long serialVersionUID = 1L;
/**
- 主键
*/
@TableId(type = IdType.ID_WORKER_STR)
private String oid;
/**
- 文章标题
*/
@NotNull(message = "标题不能为空")
private String articleTitle;
/**
- 文章摘要
*/
private String articleSummary;
/**
- 文章标签,英文逗号分隔
*/
private String articleTags;
/**
- 文章作者id
*/
private String articleAuthorId;
/**
- _count int(11)
*/
private Integer articleCommentCount;
/**
- 文章浏览计数
*/
private Integer articleViewCount;
/**
- 文章正文
*/
private String articleContent;
/**
- 文章访问路径
*/
private String articlePermalink;
/**
- 文章是否置顶
*/
private String articlePutTop;
/**
- 文章创建时间
*/
private Date articleCreateTime;
/**
- 文章更新时间
*/
private Date articleUpdateTime;
/**
- able char(1)
*/
private String articleCommentable;
/**
- 文章首图地址
*/
private String articleImgUrl;
/**
- 文章状态,0-已发布,1-草稿
*/
private Integer articleStatus;
}
Controller:
/**
* [新增]
*
* @author Na7han
* @date 2019/11/26
**/
@ResponseBody
@RequestMapping("/insert")
public ReturnT insert(@Valid LuteArticle luteArticle, BindingResult result) {
if (result.hasErrors()) {
return ReturnT.FAIL(HttpStatus.HTTP_BAD_REQUEST, Objects.requireNonNull(result.getFieldError()).getDefaultMessage());
}
return luteArticleService.insert(luteArticle);
}