使用@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://blog.csdn.net/FU250/article/details/80208261

代码分析,日期格式的参数建议使用时间戳传递,以上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}}
最后完结,由于篇幅有限,下次会以这个实例为基础,实现一个自定义的注解实现,该篇文章到此结束,阅读有任何问题请及时反馈

猜你喜欢

转载自blog.csdn.net/fu250/article/details/80247930