spring 注解@Validated验证大于或小于浮点数使用方法

spring 注解@Validated验证大于或小于浮点数使用方法

    • 1.引入依赖
    • 2.在需要添加注解的dto类中添加@DecimalMax或@DecimalMin注解
    • 3.在controller层方法参数中添加@Validated注解
      • 参考链接

1.引入依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

2.在需要添加注解的dto类中添加@DecimalMax或@DecimalMin注解

import java.io.Serializable;
import java.math.BigInteger;

import lombok.Data;

import javax.validation.constraints.*;


@Data
public class RedEnvelopDto implements Serializable {

    private static final long serialVersionUID = 1L;
    @NotNull(message = "不能为空")
    @DecimalMin(value = "0.01",message = "红包金额必须大于或等于0.01")
    @DecimalMax(value = "10000" ,message = "红包金额必须小于或等于10000")
    private String totalMoney;

    @NotNull(message = "不能为空")
    private BigInteger sendId;

}

3.在controller层方法参数中添加@Validated注解

@PostMapping(value = "/api/setred")
    public Result<Object> setRed(@Validated @RequestBody RedEnvelopDto redEnvelopDto) {
          
            return null;
    }

参考链接

https://blog.csdn.net/qq920447939/article/details/80198438

你可能感兴趣的:(@Validated,spring,boot,java)