Parameter validation without global validation can result in very bloated code. There are a large number of if judgment non-null statements. Here’s an elegant solution. I’ll start with a later annotation, @initBinder, that does:

@initBinder initializes Binder. The annotated method initializes WebDataBinder. WebDataBinder is used for form-to-method data binding.

@initBinder only annotates methods in @Controller to register a binder initialization method for the Controller, which is valid only for the Controller.

@InitBinder public void initBinder(WebDataBinder webDataBinder){ //TODO

}

0x01: Pom.xml introduces the dependency library

The < project XMLNS = "http://maven.apache.org/POM/4.0.0" XMLNS: xsi = "http://www.w3.org/2001/XMLSchema-instance" Xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion > 4.0.0 < / modelVersion > < groupId > com. Olive < / groupId > < artifactId > valid - demo < / artifactId > < version > 0.0.1 - the SNAPSHOT < / version > < packaging > jar < / packaging > < the parent > < groupId > org. Springframework. Boot < / groupId > <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.8.RELEASE</version> <relativePath /> </parent> <name>valid-demo</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> </dependency> </dependencies> </project>Copy the code

Although it references the @initBinder annotation, the underlying framework still uses SpringBoot’s validation framework

#0x02: Define the Vo object

Define a BaseVo class to facilitate global parameter determination. This class is very simple and the code is as follows

package com.olive.vo;

import java.io.Serializable;

public class BaseVo implements Serializable{

}
Copy the code

Define the query parameter class UserQueryVo, inherited from BaseVo

package com.olive.vo; import javax.validation.constraints.NotEmpty; Public class UserQueryVo extends BaseVo {@notempty (message=" cannot be empty ") private String query; public String getQuery() { return query; } public void setQuery(String query) { this.query = query; }}Copy the code

The key is to use annotations in the fields to indicate that the parameters cannot be empty

0x03: Define a controller

package com.olive.controller; import java.util.HashMap; import java.util.Map; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.olive.vo.UserQueryVo; @RestController public class UserController { @RequestMapping("/user/queryUser") public Map queryUser(@RequestBody @Validated UserQueryVo queryVo){ Map result = new HashMap(); result.put("code", 200); result.put("msg", "success"); return result; }}Copy the code

Use the @Validated annotation in the parameter to verify the Vo class. In fact, this step is normal to complete the parameter verification, but there is no unified interception entry.

0x04: Adds unified interception entry for parameter verification

package com.olive; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.validation.BeanPropertyBindingResult; import org.springframework.validation.ObjectError; import org.springframework.validation.Validator; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestControllerAdvice; import com.olive.vo.BaseVo; @RestControllerAdvice public class UserControllerAdvice { @Autowired private Validator validator; @InitBinder public void initValid(WebDataBinder webDataBinder){ Object target= webDataBinder.getTarget(); if(target==null){ return; } if(target instanceof BaseVo){ BeanPropertyBindingResult result = new BeanPropertyBindingResult(target, target.getClass().getSimpleName()); validator.validate(target, result); List<ObjectError> errors = result.getAllErrors(); if(errors==null || errors.isEmpty()){ return; } throw new RuntimeException(" parameter error "); } } @ExceptionHandler(value = Exception.class) @ResponseBody public Map defaultErrorHandler(HttpServletRequest req, Exception e) { Map result = new HashMap(); result.put("code", 500); result.put("msg", e.getMessage()); return result; }}Copy the code

The code is analyzed below

  • Identified with @RestControllerAdvice, this annotation intercepts controllers tagged with @RestController

  • In the initValid method, use the @initbinder identifier; At the same time, the method passes in a WebDataBinder object and writes the parameter verification code in the method. Throw an exception if validation fails. This throws a RuntimeException; In a real project, you can customize the parameter exception class ParamValidExecpiton that inherits RuntimeException

  • In the method defaultErrorHandler, the @ExceptionHandler identifier is used; Global Exception handling, where Exception is intercepted directly; In actual projects, you can directly intercept your own parameter exception class ParamValidExecpiton.

0x05: Write the boot class

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main( String[] args ) { SpringApplication.run(Application.class, args); }}Copy the code

Start and validate

Mmbiz. Qpic. Cn/mmbiz_png/g…

Debug mode can be seen

In this way, unified control parameter verification is achieved without the need to split into different code blocks.