Java-List的对象的校验不起作用的解决方案
记录工作学习中的点点滴滴~
在spring框架下,使用 javax.validation.* 校验参数的时候,对 java.util.List 的对象的校验不起作用。
请求方式 POST
请求参数格式 application/json
package com.tsing0520.config; import org.springframework.validation.BindException; import org.springframework.validation.FieldError; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; @ControllerAdvice public class GlobalExceptionHandler { /** * 参数校验异常处理 (@RequestParam注解抛出) * @param ex * @return */ @ExceptionHandler(BindException.class) @ResponseBody public String handleBindException(BindException ex) { StringBuilder stringBuilder = new StringBuilder(); for (FieldError error : ex.getBindingResult().getFieldErrors()){ stringBuilder.append(error.getDefaultMessage()).append(","); } String msg = stringBuilder.substring(0, stringBuilder.length() -1); return msg; } @ExceptionHandler(MethodArgumentNotValidException.class) @ResponseBody public String validException(MethodArgumentNotValidException ex) { StringBuilder stringBuilder = new StringBuilder(); for (FieldError error : ex.getBindingResult().getFieldErrors()) { stringBuilder.append(error.getDefaultMessage()).append(","); } String msg = stringBuilder.substring(0, stringBuilder.length() -1); return msg; } }
package com.tsing0520.controller; import javax.validation.Valid; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.tsing0520.model.param.PersonListParam; import com.tsing0520.model.param.PersonParam; import cn.hutool.json.JSONUtil; @ResponseBody @Controller @RequestMapping("/verifyparam") public class ValidateParamController { @PostMapping("/uploadPerson") public String uploadPerson(@RequestBody @Valid PersonParam personParam) { System.out.println(JSONUtil.toJsonStr(personParam)); return "uploadPerson...success"; } @PostMapping("/batchUploadPerson") public String batchUploadPerson(@RequestBody @Valid PersonListParam personListParam) { System.out.println(JSONUtil.toJsonStr(personListParam)); return "batchUploadPerson...success"; } }
package com.tsing0520.model.param; import javax.validation.constraints.Min; import javax.validation.constraints.NotBlank; import lombok.Data; @Data public class PersonParam { @Min(value=1,message="id最小值为1") private Integer id; @NotBlank(message="name不能为空") private String name; }
未起作用的代码
package com.tsing0520.model.param; import java.util.List; // import javax.validation.Valid; import javax.validation.constraints.NotEmpty; import lombok.Data; @Data public class PersonListParam { // @Valid @NotEmpty(message="personParamList不能为空") List<PersonParam> personParamList; }
{
"personParamList": [
{
"name": "abc",
"id": 0
},
{
"name": "abc",
"id": 0
}
]
}
修改之后的代码
package com.tsing0520.model.param; import java.util.List; import javax.validation.Valid; import javax.validation.constraints.NotEmpty; import lombok.Data; @Data public class PersonListParam { @Valid @NotEmpty(message="personParamList不能为空") List<PersonParam> personParamList; }