SpringBoot ~ 数据校验

  1. 云栖社区>
  2. 博客>
  3. 正文

SpringBoot ~ 数据校验

wsyjlly 2019-08-12 13:59:46 浏览140
展开阅读全文

SpringBoot数据校验

  1. 添加pom依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>
  2. 编写实体类

    /**
     * @author wsyjlly
     * @create 2019.06.29 - 12:18
     **/
    @AllArgsConstructor
    @NoArgsConstructor
    @Accessors
    @Data
    @TableName(value = "message")
    public class Message implements Serializable {
        private Integer id;
        private Integer mid;
    
        @Size(min = 5,max = 10,message = "{message.content.size}")
        @NotNull(message = "{message.content.notnull}")
        private String content;
    
        @Size(min = 5,max = 10,message = "{message.column1.size}")
        @NotNull(message = "{message.column1.notnull}")
        @TableField(value = "column_200")
        private String column1;
    
        @Size(min = 5,max = 10,message = "{message.column2.size}")
        @NotNull(message = "{message.column2.notnull}")
        @TableField(value = "column_500")
        private String column2;
    
        @Size(min = 5,max = 10,message = "{message.column3.size}")
        @NotNull(message = "{message.column3.notnull}")
        @TableField(value = "column_5000")
        private String column3;
    }
  3. ValidationMessages.properties配置

    message.content.notnull=该字段不能为空
    message.column1.notnull=该字段不能为空
    message.column2.notnull=该字段不能为空
    message.column3.notnull=该字段不能为空
    message.content.size=内容长度过短
    message.column1.size=内容长度过短
    message.column2.size=内容长度过短
    message.column3.size=内容长度过短

    1563358182687

  4. Controller校验

    /**
     * @author wsyjlly
     * @create 2019.07.17 - 17:34
     **/
    @RestController
    @RequestMapping("/message")
    public class MessageController {
        @PostMapping("/validate")
        public List validate(@RequestBody @Validated Message message, BindingResult result){
            System.out.println(message);
            ArrayList<Object> list = new ArrayList<>();
            if (result.hasErrors()) list.addAll(result.getAllErrors());
            for (ObjectError item : result.getAllErrors()){
                System.out.println(item);
            }
            list.add(message);
            return list;
        }
    }
  5. 分组校验

    • 创建分组接口

      /**
       * @author wsyjlly
       * @create 2019.07.17 - 18:29
       **/
      @Component
      public interface ValidationGroup1 {}
      /**
       * @author wsyjlly
       * @create 2019.07.17 - 18:29
       **/
      @Component
      public interface ValidationGroup2 {}
    • 实体类添加分组

      /**
       * @author wsyjlly
       * @create 2019.06.29 - 12:18
       **/
      @AllArgsConstructor
      @NoArgsConstructor
      @Accessors
      @Data
      @TableName(value = "message")
      public class Message implements Serializable {
          private Integer id;
          private Integer mid;
      
          @Size(min = 5,max = 10,message = "{message.content.size}")
          @NotNull(message = "{message.content.notnull}")
          private String content;
      
          @Size(min = 5,max = 10,message = "{message.column1.size}")
          @NotNull(message = "{message.column1.notnull}")
          @TableField(value = "column_200")
          private String column1;
      
          @Size(min = 5,max = 10,message = "{message.column2.size}",groups = ValidationGroup1.class)
          @NotNull(message = "{message.column2.notnull}")
          @TableField(value = "column_500")
          private String column2;
      
          @Size(min = 5,max = 10,message = "{message.column3.size}",groups = {ValidationGroup1.class, ValidationGroup2.class})
          @NotNull(message = "{message.column3.notnull}")
          @TableField(value = "column_5000")
          private String column3;
      }
    • Controller校验指定分组

      /**
       * @author wsyjlly
       * @create 2019.07.17 - 17:34
       **/
      @RestController
      public class ValidateController {
      
          @PostMapping("/validate1")
          public List validate(@RequestBody @Validated Message message, BindingResult result){
              System.out.println(message);
              ArrayList<Object> list = getObjects(result);
              list.add(message);
              return list;
          }
          
          @PostMapping("/validate2")
          public List validate2(@RequestBody @Validated(ValidationGroup1.class) Message message, BindingResult result){
              System.out.println(message);
              ArrayList<Object> list = getObjects(result);
      list.add(message);
              return list;
          }
          
          @PostMapping("/validate3")
          public List validate3(@RequestBody @Validated(ValidationGroup2.class) Message message, BindingResult result){
              System.out.println(message);
              ArrayList<Object> list = getObjects(result);
              list.add(message);
              return list;
          }
      
          private ArrayList<Object> getObjects(BindingResult result) {
              ArrayList<Object> list = new ArrayList<>();
              if (result.hasErrors()) {
                  for (ObjectError item : result.getAllErrors()) {
                      list.add(item.getDefaultMessage());
                      System.out.println(item.getDefaultMessage());
                  }
              }
              return list;
          }
      }

网友评论

登录后评论
0/500
评论
wsyjlly
+ 关注