Usage examples of SpringBoot annotation @Valid

Use @Valid annotation for request parameter verification in SpringBoot interface project

Preface

In the development of our interface project, parameter verification is inevitable when request parameters are involved. If there are only one or two request parameters, we can directly verify them through if else in the controller. Then when there are more parameters If we still use if else to verify,
the code does not seem to look very good when written. So in order to solve this problem and make our code more concise and efficient, @Valid is used here to verify the request parameters.

text

1. Introduce Maven dependencies

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

2. Modify the request parameter entity class

Specify the parameters that need to be verified through relevant annotations on the request parameter entity and return the corresponding verification information. Just look at the code first.

package com.test.demo.entity;

import lombok.Data;

import javax.validation.constraints.NotBlank;

/**
 * @Description:  测试实体
 * @Author:       Olrookie
 * @Date:         2023/5/23
 */
@Data
public class TestEntity {
    
    

    /**
     * 参数值
     */
    private String strKey;

    /**
     * 参数值
     */
    private String strValue;

    /**
     * appCode身份验证校验
     */
    @NotBlank(message = "参数appCode为必填参数!")
    private String appCode;

    /**
     * 页编号
     */
    @NotBlank(message = "参数pageNum,pageSize为必填参数!")
    private String pageNum;

    /**
     * 页大小
     */
    @NotBlank(message = "参数pageNum,pageSize为必填参数!")
    private String pageSize;

}

Here we use @NotBlank to verify the three parameters appCode, pageNum, and pageSize and indicate the message returned when the verification fails. Here @NotBlank specifies that the request parameter cannot be null or empty, except for @NotBlank There are also some other annotations, as shown in the following table:

annotation describe
@Null Limit can only be null
@NotNull limit must not be null
@AssertFalse limit must be false
@AssertTrue limit must be true
@DecimalMax(value) The limit must be a number no larger than the specified value
@DecimalMin(value) The limit must be a number no less than the specified value
@Digits(integer,fraction) The limit must be a decimal, and the number of digits in the integer part cannot exceed integer, and the number of digits in the decimal part cannot exceed fraction.
@Future The limit must be a future date
@Max(value) The limit must be a number no larger than the specified value
@Min(value) The limit must be a number no less than the specified value
@Past The limit must be a date in the past
@Pattern(value) The restriction must match the specified regular expression
@Size(max,min) The limit character length must be between min and max
@Past Verify that the annotation element value (date type) is earlier than the current time
@NotEmpty Verify that the element value of the annotation is not null and not empty (the string length is not 0 and the collection size is not 0)
@NotBlank Verify that the element value of the annotation is not empty (not null, and the length is 0 after removing the first space). Unlike @NotEmpty, @NotBlank only applies to strings and will remove spaces from the strings during comparison.
@Email The element value of the verification annotation is Email. You can also specify a custom email format through regular expressions and flags.

3. Use the annotation @Valid and return verification failure information

Controller code detailed explanation

Adding the annotation @Valid in front of the interface entity class can verify the request parameters. So what if we want to return verification failure information?
Then we need to add the parameter BindingResult to the method, use it to receive the verification failure information and return

Detailed explanation of the Controller code below:
1. First, we judge the BindingResult through the public method paramCheck to determine whether it contains verification failure information. If it does not contain verification failure information, further verify whether the request parameter appCode is correct and return relevant information (here we follow The content discussed today is not relevant and will not be explained further)
2. If the BindingResult contains verification failure information, it will be returned
. 3. The content returned here uses the unified CommonResult class.

package com.test.demo.controller;

import com.test.cloudapicommons.common.CommonResult;
import com.test.cloudapicommons.entities.BaseInfo;
import com.test.cloudapicommons.entities.ResponseCode;
import com.test.cloudapicommons.utils.JudgeUtils;
import com.test.demo.entity.TestEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @Description:  Demo1测试Controller
 * @Author:       Olrookie
 * @Date:         2023/5/22
 */
@Slf4j
@RestController
@RequestMapping("/api")
public class Demo1Controller {
    
    

    @GetMapping("/testDemo1")
    public CommonResult test(@Valid TestEntity testEntity, BindingResult bindingResult) {
    
    

        String paramCheckMsg = JudgeUtils.paramCheck(bindingResult);
        // 请求参数校验
        if ("success".equals(paramCheckMsg)) {
    
    
            // appCode校验
            if (JudgeUtils.identityCheck(testEntity.getAppCode())) {
    
    
                return new CommonResult(ResponseCode.RESULT_CODE_SUCCESS, BaseInfo.SUCCESS_INFO);
            }
            log.warn("appCode校验失败! 请求的错误appCode为:{}", testEntity.getAppCode());
            return new CommonResult(ResponseCode.RESULT_CODE_FALIER_IDENTITY, "appCode错误!");
        }
        return new CommonResult(ResponseCode.RESULT_CODE_PARAM_ERROR, paramCheckMsg);

    }
}

Detailed explanation of paramCheck judgment method

The paramCheck method first determines whether the BindingResult contains verification failure information. If it does, it will return the first error message by default. If successful, it will return "success"

package com.test.cloudapicommons.utils;
import com.test.cloudapicommons.entities.BaseInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;

/**
 * @Description:  提取用于判断的公共方法
 * @Author:       Olrookie
 * @Date:         2023/5/23
 */
@Slf4j
public class JudgeUtils {
    
    

    /**
     *                 判断请求参数appCode是否正确
     * @param appCode  身份验证appCode
     * @return         true/false
     */
    public static Boolean identityCheck(String appCode) {
    
    
        return BaseInfo.APP_CODE.equals(appCode);
    }

    /**
     *                       返回实体类校验信息
     * @param bindingResult  BindingResult
     * @return               errorMsg/success
     */
    public static String paramCheck(BindingResult bindingResult) {
    
    
        // 通过BinidingResult绑定实体类校验信息
        if (bindingResult.hasErrors()) {
    
    
            // 如果实体类校验出现错误,则返回第一条错误信息的默认msg
            String errorMsg = bindingResult.getAllErrors().get(0).getDefaultMessage();
            log.warn("请求参数未通过校验:{}", errorMsg);
            return errorMsg;
        }
        return "success";
    }
}

Unified return information

This encapsulates the return content in a unified format for the entire project, which includes response codes, response logs and specific return content.

package com.test.cloudapicommons.common;

import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @Description:  统一给前端参数
 * @Author:       Olrookie
 * @Date:         2023/5/23
 */
@Data
@NoArgsConstructor
public class CommonResult <T> {
    
    

    /** 响应码 */
    private String code;
    /** 响应日志 */
    private String message;
    /** 实体类 */
    private T data;

    /**
     *
     * @param code     响应码
     * @param message  具体响应日志
     */
    public CommonResult(String code, String message) {
    
    
        this.code = code;
        this.message = message;
    }

    /**
     *
     * @param code     响应码
     * @param message  具体响应日志
     * @param data     具体返回的实体类
     */
    public CommonResult(String code, String message, T data) {
    
    
        this.code = code;
        this.message = message;
        this.data = data;
    }
}

4. Interface testing

The following is the interface returned by sending requests with different parameters through ApiPost

4.1 Return verification failure information of request parameters

Insert image description here
Insert image description here

4.2 Information returned after successful request

Insert image description here

Conclusion

Recently, I have just encapsulated a SpringBoot interface parent project. Combining the previous interface development issues, I came up with the annotation @Valid, so I wrote this article here for record, and I hope it will be helpful to everyone. If you have any questions, please feel free to comment. I hope you will give me some advice and communicate more. Finally, I wish everyone becomes stronger!

おすすめ

転載: blog.csdn.net/weixin_55549435/article/details/131109720