• 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;
    }

  • 相关阅读:
    反向传播BP算法
    有关一些求导的概念与神经网络梯度下降
    CS224n学习笔记(二)
    CS224n学习笔记(一)
    【模板】动态主席树
    【模板】静态主席树
    【模板】替罪羊树
    【模板】Splay
    洛谷 2387/BZOJ 3669 魔法森林
    洛谷 2622 关灯问题Ⅱ
  • 原文地址:https://www.cnblogs.com/tsing0520/p/14166429.html
Copyright © 2020-2023  润新知