An Errors/BindingResult argument is expected to be declared immediately after the model attribute, the @RequestBody or the @RequestPart arguments to which they apply

2018/11/28 posted in  SpringBoot

今天测试接口的时候,出现了这个错误

An Errors/BindingResult argument is expected to be declared immediately after the model attribute, the @RequestBody or the @RequestPart arguments to which they apply

我是后台验证参数的正确性,用了@Valid 注解,结果报了上述错误。
方法如下:

 @PostMapping("/dept")
    @ApiOperation(value="添加部门", notes = "添加部门")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyName", value = "关联公司", required = false, dataType = "String")
    })
    public JsonResult addDept(BindingResult bindingResult,@Valid Dept dept, @ApiIgnore @RequestAttribute User currentUser,String companyName){
        if(bindingResult.hasErrors()){
            return JsonResult.renderFail(bindingResult.getFieldError().getDefaultMessage());
        }

后来把@Valid实体放在BindingResult前面就行了

 @PostMapping("/dept")
    @ApiOperation(value="添加部门", notes = "添加部门")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyName", value = "关联公司", required = false, dataType = "String")
    })
    public JsonResult addDept(@Valid Dept dept, BindingResult bindingResult,@ApiIgnore @RequestAttribute User currentUser,String companyName){
        if(bindingResult.hasErrors()){
            return JsonResult.renderFail(bindingResult.getFieldError().getDefaultMessage());
        }