使用@Valid+BindingResult 进行controller参数校验

由于controller是调用的第一层,经常参数校验将在这里完成,常见有非空校验、类型校验等,常见写法为以下伪代码:

public void round(Object a){
if(a.getLogin() == null){
   return "手机号不能为空!";
 }
}

但是调用对象的位置会有很多,而且手机号都不能为空,那么我们会想到把校验方法抽出来,避免重复的代码。但有框架支持我们通过注解的方式进行参数校验。

先立个场景,为往动物园添加动物,动物对象如下,时间节点大概在3030年,我们认为动物可登陆动物专用的系统,所以有password即自己的登录密码。

public class Animal {
  private String name;
  private Integer age;
  private String password;
  private Date birthDay;
}

调用创建动物的controller层如下,简洁明了,打印下信息后直接返回。

@RestController
@RequestMapping("/animal")
public class AnimalController {
 @PostMapping
  public Animal createAnimal(@RequestBody Animal animal){
      logger.info(animal.toString());
      return animal;
  }
}

伪造Mvc调用的测试类。

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestAnimal {

  private final static Logger logger = LoggerFactory.getLogger(TestAnimal.class);
  @Autowired
  private WebApplicationContext wac;
  private MockMvc mockMvc;

  @Before
  public void initMock(){
      mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
  }

  @Test
  public void createAnimal() throws Exception {
      String content = "{\"name\":\"elephant\",\"password\":null,\"birthDay\":"+System.currentTimeMillis()+"}";
      String result = mockMvc.perform(MockMvcRequestBuilders.post("/animal")
              .content(content)
              .contentType(MediaType.APPLICATION_JSON_UTF8))
              .andExpect(MockMvcResultMatchers.status().isOk())
              .andReturn().getResponse().getContentAsString();
      logger.info(result);
  }
}

以上代码基于搭建的springboot项目,想搭建的同学可以参考搭建篇 https://www.jb51.net/article/226998.htm

代码分析,日期格式的参数建议使用时间戳传递,以上birthDay传递 "2018-05-08 20:00:00",将会抛出日期转换异常,感兴趣的同学可以试试。

由于密码很重要,现在要求密码为必填,操作如下,添加@NotBlank注解到password上:

@NotBlank
private String password;

但光加校验注解是不起作用的,还需要在方法参数上添加@Valid注解,如下:

@Valid @RequestBody Animal animal

此时执行测试方法,抛出异常,返回状态为400:

java.lang.AssertionError: Status
Expected :200
Actual :400
<Click to see difference>


at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:54)
at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:81)

 

说明对password的非空校验已经生效了,直接抛出异常。如果不想抛出异常,想返回校验信息给前端,这个时候就需要用到BindingResult了,修改创建动物的方法,添加BindingResult参数:

@PostMapping
  public Animal createAnimal(@Valid @RequestBody Animal animal, BindingResult bindingResult){
      if (bindingResult.hasErrors()){
          bindingResult.getAllErrors().forEach(o ->{
              FieldError error = (FieldError) o;
              logger.info(error.getField() + ":" + error.getDefaultMessage());
          });
      }
      logger.info(animal.toString());
      return animal;
  }

此时,执行测试,可以看到日志中的错误信息:

2018-05-09 00:59:37.453 INFO 14044 --- [ main] c.i.s.d.web.controller.AnimalController : password:may not be empty

为了满足我们编码需要我们需要进行代码改造,1.不能直接返回animal。2.返回的提示信息得是用户可读懂的信息。

controller方法改造如下,通过Map对象传递请求成功后的信息或错误提示信息。

@PostMapping
  public Map<String,Object> createAnimal(@Valid @RequestBody Animal animal, BindingResult bindingResult){
      logger.info(animal.toString());
      Map<String,Object> result = new HashMap<>();
      if (bindingResult.hasErrors()){
          FieldError error = (FieldError) bindingResult.getAllErrors().get(0);
          result.put("code","400");//错误编码400
          result.put("message",error.getDefaultMessage());//错误信息
          return result;
      }
      result.put("code","200");//成功编码200
      result.put("data",animal);//成功返回数据
      return result;
  }

返回的密码提示信息如下:

@NotBlank(message = "密码不能为空!")
private String password;

执行测试方法,返回结果

com.imooc.security.demo.TestAnimal : {"code":"400","message":"密码不能为空!"}

最后贴一个,设置password值返回成功的信息

com.imooc.security.demo.TestAnimal : {"code":"200","data":{"name":"elephant","age":null,"password":"lalaland","birthDay":1525799768955}}

由于篇幅有限,下次会以这个实例为基础,实现一个自定义的注解实现,该篇文章到此结束。

 

Controller层方法的参数校验

import com.example.demo.pojo.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; 
import javax.validation.Valid;

@Controller
@RequestMapping("/stu")
public class StudentController { 
  @PostMapping("/addStu")
  @ResponseBody
  public String addStudent(@Valid Student student){
      System.out.println("存储student对象");
      System.out.println(student);
      return "ok";
  } 
}
import org.hibernate.validator.constraints.Length; 
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

public class Student { 
  @NotNull(message = "传入的是空值,请传值")
  @Min(value = 0,message = "传入学生分数有误,分数在0-100之间")
  @Max(value = 100,message = "传入学生分数有误,分数在0-100之间")
  private Integer score;

  @NotEmpty(message = "传入的是空字符串,请传值")
  @NotNull(message = "传入的是空值,请传值")
  private String name;

  @NotNull(message = "传入的是空值,请传值")
  @NotEmpty(message = "传入的是空字符串,请传值")
  @Length(min = 11,max = 11,message = "号码有误,长度应为11位")
  private String mobile;

  public Integer getScore() {
      return score;
  }

  public void setScore(Integer score) {
      this.score = score;
  }

  public String getName() {
      return name;
  }

  public void setName(String name) {
      this.name = name;
  }

  public String getMobile() {
      return mobile;
  }

  public void setMobile(String mobile) {
      this.mobile = mobile;
  }

  @Override
  public String toString() {
      return "Student{" +
              "score=" + score +
              ", name='" + name + '\'' +
              ", mobile='" + mobile + '\'' +
              '}';
  }
}

全局统一异常拦截器

import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody; 

@ControllerAdvice
public class GlobalExceptionInterceptor { 
  @ExceptionHandler(value = Exception.class)
  @ResponseBody
  public String exceptionHandler(Exception e){
      String failMessage=null; 
      if(e instanceof BindException){
          failMessage=((BindException) e).getBindingResult().getFieldError().getDefaultMessage();
      }
      return failMessage;
  } 
}

当我们传入的参数有误时,就会被异常拦截器捕获,返回给我们错误信息。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程宝库

在企业系统的开发中,用户表单输入的场景是会经常遇见的,如何让数据校验脱离于业务代码逻辑,谁也不想在逻辑代码里对字段逐一判断。Spring MVC的校验方式在使用Spring MVC时的时候, ...