@Validated注解的验证功能

添加maven依赖

<dependency>
  <groupId>javax.validation</groupId>
  <artifactId>validation-api</artifactId>
</dependency>

 

实体类

package com.abi.common.request;

import lombok.Data;

import javax.validation.constraints.*;

/**
 * @author ytlStar
 * @ClassName
 * @Date 2022-06-29 14:59
 */
@Data
public class Alerts {

    @NotBlank(message = "不可为空",groups = {InsertGroup.class})
    private String username;

    @NotBlank(message = "不可为空",groups = {InsertGroup.class,UpdateGroup.class})
    private String password;

    @Future(message = "时间要在当前时间之后才可以通过")
    private Data data;

    @AssertTrue(message = "字段必须为true")
    private boolean isSave;

    @Max(value = 20, message = "最大长度为20")
    private String address;

    @Pattern(regexp = "^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*\\.[a-zA-Z0-9]{2,6}$", message = "不满足邮箱正则表达式",groups = {InsertGroup.class})
    private String email;

    @Min(value = 1,message = "年龄不能小于1岁")
    @Max(value = 100,message = "年龄不能大于100岁")
    private int age;

    @NotNull
    @Size(max = 10, min = 5, message = "字段长度要在5-10之间")
    private String fileName;

    @Pattern(regexp = "^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\\d{8}$",message = "手机号码有误!",groups = {UpdateGroup.class})
    private String phone;

    public interface InsertGroup{
        //定义空接口仅为分组标记  接口本身不具备实意
    }

    public interface UpdateGroup{

    }
}

控制器:

@Slf4j
@Api(tags = "日志服务动态锁定接口")
@RestController
@RequestMapping("/api/sls")
@RequiredArgsConstructor
public class SlsResultController {

    private final SlsResultService slsResultService;

    @ApiOperation("")
    @PostMapping("/insert")
    public ApiResponse<List<Receivers>> accept(@RequestBody @Validated(Alerts.InsertGroup.class) Alerts alerts, BindingResult bindingResult) {
        if (bindingResult.hasErrors()){
            List<ObjectError> allErrors = bindingResult.getAllErrors();

        }
        return ApiResponse.result(this.slsResultService.accept(alerts));
    } 
    
    @PostMapping("/update")
    public ApiResponse<List<Receivers>> depend(@RequestBody @Validated(Alerts.UpdateGroup.class) Alerts alerts, BindingResult bindingResult) {
        if (bindingResult.hasErrors()){
            List<ObjectError> allErrors = bindingResult.getAllErrors();

        }
        return ApiResponse.result(this.slsResultService.accept(alerts));
    }
}

补充说明

(1)@NotEmpty

The annotated element must not be {@code null} nor empty.
不能是null
不能是空字符
集合框架中的元素不能为空

(2)@NotNul

被修饰元素不能为null,可用在基本类型上

(3)@NotBlank

The annotated element must not be {@code null} and must contain at least one non-whitespace character.

该注解用来判断字符串或者字符,只用在String上面

如果在基本类型上面用@NotEmpty或者@NotBlank,会出现以下错误

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'javax.validation.constraints.NotBlank' validating type 'java.lang.Long'. Check configuration for 'offset'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值