Springboot Controller单参数校验

Springboot使用JSR-303 Validation进行验证,大部分博客都只提及对Controller中的dto对象进行属性校验。本文是介绍如何对Controller中的方法的单一参数进行校验,步骤如下。

1. Controller添加注解@Validated

@RestController
@RequestMapping("/test")
@Validated
public class TestController {}

2. 单一参数校验

使用@Min等标签校验参数。

    @PostMapping("/list")
    @ResponseBody
    public Result getBookList(@Min(1) Integer pageNum,@Min(1) Integer pageSize, @RequestBody @Validated TestDto dto) {
        ...
    }

参考文献

Bean Validation 技术规范特性概述
)


本文作者: seawish
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 许可协议。转载请注明出处!

你可能感兴趣的:(Springboot Controller单参数校验)