Getting a @ParamsValidate

@ParamsValidate series of tutorials  https://blog.csdn.net/u010606397/article/category/7689866

web backend verification request parameter is a very commonly used functions, java web to the widespread use of spring validation check. I also wrote a library params-validate a check request parameters, compared with the spring validation still some small advantages, such as:

1, does not require changes pojo class.

2, when the map using the received parameters, the parameters can be checked.

 

Create a new controller method:

public Object demo1(@RequestBody User user){}
//User类
public class User {
    private String id;
    private String name;
    private String birthday;
    private Boolean single;
    private Float salaryNum;
    private BigDecimal bigNum;

    限于篇幅,省略一大堆get、set方法
}

Parameter passing of the front end of the following requirements:

name: only letters, numbers, _, the required minimum length of 10, the maximum length 100
Birthday: Required
single: only a Boolean
salaryNum: only positive numbers, required, a minimum of 100, the maximum value 1000
Bignum : it can only be positive or 0


May be implemented using params-validate this check functions.

1, import-dependent

<dependency>
	<groupId>com.github.codingsoldier</groupId>
	<artifactId>params-validate</artifactId>
	<version>1.8-RELEASE</version>
	<!--版本不能低于1.8,最新版本请查看:
	https://mvnrepository.com/artifact/com.github.codingsoldier/params-validate -->
	<exclusions>
		<!--排除重复的依赖-->
		<exclusion>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
		</exclusion>
	</exclusions>
</dependency>

2, ValidateInterfaceAdapter new subclass. ValidateInterfaceAdapter is a virtual class, must create a new subclass can be instantiated, and injected into the vessel IOC.

@Component
public class ValidateInterfaceImpl extends ValidateInterfaceAdapter{

}

3, a new directory in the resources folder validate ( folder name must be the validate ), create a new file json learn-params-validate.json (json file name can be freely written) at the validate, as red arrows in FIG.

Fill in the following contents learn-params-validate.json:

{
  "userValidate": {
    "name": {
      "request": true,
      "minLength": 10,
      "maxLength": 100,
      "regex": "^[a-zA-Z0-9_]+$",
      "message": "名字必须是字母、数字、_"
    },
    "birthday": {
      "request": true
    },
    "single": {
      "regex": "^(true)$|^(false)$",
      "message": "single必须是true、false"
    },
    "salaryNum": {
      "request": "true",
      "minValue": 100,
      "maxValue": 1000,
      "regex": "^(\\+)?\\d+(\\.\\d+)?$",
      "message": "薪水必须是正数"
    },
    "bigNum": {
      "regex": "^(\\+)?\\d+(\\.\\d+)?$",
      "message": "bigNum必须正数或0"
    }
  }
}

4, the new controller

@RestController
@RequestMapping("/learn")
public class LearnParamsValidate {

    @PostMapping("/demo1")
    /**
     * 添加校验注解@ParamsValidate
     * file的值是第三步的json文件learn-params-validate.json
     * key的值是learn-params-validate.json中的userValidate
     */
    @ParamsValidate(file = "/learn-params-validate.json", key = "userValidate")
    public Object demo1(@RequestBody User user){
        System.out.println("demo1方法执行");
        Map<String, Object> map1 = new HashMap<>();
        map1.put("code", 0);
        map1.put("data", "成功");
        return map1;
    }
}

Send a request using the postman, as shown below:

Remember the requirements for front-end parameter passing it? Requirements are as follows:

name: only letters, numbers, _, the required minimum length of 10, the maximum length 100
Birthday: Required
single: only a Boolean
salaryNum: only positive numbers, required, a minimum of 100, the maximum value 1000
Bignum : it can only be positive or 0

Only a single front-end pass parameters to meet the requirements, other parameters do not meet. The editor requests console and results can be seen postman controller.demo1 () method is not performed. This is the reason for the request is intercepted @ParamsValidate tip parameter passing are not met, the method does not execute controller, the return parameter verification information to the head end.

The following parameters change correctly, Zaifayici request, by the following diagram, the editor can see the console controller.demo1 () method is executed.

Thus, a simple method to use params-validate introduced over.

Let me elaborate rules under json file checksum

1, a new json file in the directory validate. All live parameter with a key request key frame.

用userValidate框住所有请求参数的key
"userValidate": {
    "name": {
        ...
    },
    "birthday": {
        ...
    },
    ...
}

2, write request validation rules to the parameters to be verified.

    "request": true indicates required, may be expressed to false empty, no write request may be expressed as empty
    "minValue": minimum
    "maxValue": the maximum value
    "minLength": the minimum length
    "maxLength": Maximum length
    "regex": regular expressions
    "message": argument fails to check the return message

Finally, add annotations @ParamsValidate on the controller method (file = "json file name", key = "userValidate") can be verified on the parameters.

Tutorial code   https://github.com/CodingSoldier/test-params-validate

params-validate the project address    github    code cloud  

Published 51 original articles · won praise 14 · views 40000 +

Guess you like

Origin blog.csdn.net/u010606397/article/details/86561498