• 测试开发进阶——spring boot——MVC——Spring MVC框架的数据验证——示例


    控制器:

    package com.awaimai.web;
    
    import org.hibernate.validator.constraints.*;
    import org.springframework.validation.BindingResult;
    import org.springframework.validation.annotation.Validated;
    import org.springframework.web.bind.annotation.*;
    import javax.servlet.http.Cookie;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.validation.Valid;
    import javax.validation.constraints.Max;
    import javax.validation.constraints.Min;
    import javax.validation.constraints.Size;
    import java.util.Enumeration;
    
    @RestController
    @Validated
    public class kzq
    {
    
    
        @RequestMapping(value ="/test2")
        public String test2( @Valid Student student, BindingResult bindingResult)
        {
            String a = student.getFirstName();
            String b = student.getLastName();
            return a+b;
        }
    
    
    
    
    }
    

      

    实体类:

    package com.awaimai.web;
    
    import org.springframework.validation.annotation.Validated;
    
    import javax.validation.constraints.NotNull;
    import javax.validation.constraints.Size;
    
    
    public class Student
    {
    
        @Size(min = 2,max = 5)
        private String firstName;
    
        @NotNull
        @Size(min = 2,max = 3)
        private String lastName;
    
        public void setFirstName(String firstName)
        {
            this.firstName = firstName;
        }
    
        public String getFirstName()
        {
            return firstName;
        }
    
        public void setLastName(String lastName)
        {
            this.lastName = lastName;
        }
    
        public String getLastName()
        {
            return lastName;
        }
    
    }
    

      

    postman执行:

     

     

     

     

     

  • 相关阅读:
    mysql数据库(1)
    通过全局异常处理机制实现接口参数校验返回指定返回类型
    http接口安全校验
    java 锁机制介绍
    通过反射获取类的所有属性值拼接成字符串工具类
    Mybatis中出现java.sql.SQLException: 无效的列类型: 1111
    判断两个Long相等
    jwt工具类
    mybatis #{}和${}的区别是什么
    报错解决NoSuchMethod。。。
  • 原文地址:https://www.cnblogs.com/xiaobaibailongma/p/15100935.html
Copyright © 2020-2023  润新知