zoukankan      html  css  js  c++  java
  • 测试开发进阶——spring boot——MVC——get访问——post访问——@RequestBody方法

    控制器:

    package com.awaimai.web;
    
    import org.hibernate.validator.constraints.Range;
    import org.springframework.stereotype.Controller;
    import org.springframework.validation.BindingResult;
    import org.springframework.validation.annotation.Validated;
    import org.springframework.web.bind.annotation.*;
    import org.springframework.web.servlet.ModelAndView;
    
    import javax.validation.Valid;
    import javax.validation.constraints.Max;
    import javax.validation.constraints.Min;
    import java.util.HashMap;
    import java.util.Map;
    
    
    @Controller
    @Validated
    public class kzq
    {
    
    
    
        @RequestMapping(value = "/test6")
        @ResponseBody
        public String test6(@RequestBody Student student)
        {
            String a = student.getFirstName();
            String b = student.getLastName();
            return a+b;
        }
    
    
    }
    

      

    student类:

    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访问:

    ===================================================================

    参数校验

    控制器:

    package com.awaimai.web;
    
    import org.hibernate.validator.constraints.Range;
    import org.springframework.stereotype.Controller;
    import org.springframework.validation.BindingResult;
    import org.springframework.validation.annotation.Validated;
    import org.springframework.web.bind.annotation.*;
    import org.springframework.web.servlet.ModelAndView;
    
    import javax.validation.Valid;
    import javax.validation.constraints.Max;
    import javax.validation.constraints.Min;
    import java.util.HashMap;
    import java.util.Map;
    
    
    @Controller
    @Validated
    public class kzq
    {
    
    
    
        @RequestMapping(value = "/test6")
        @ResponseBody
        public String test6(@RequestBody @Valid  Student student, BindingResult bindingResult)
        {
            String a = student.getFirstName();
            String b = student.getLastName();
            return a+b;
        }
    
    
    }
    

      

    student类:

    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访问:

  • 相关阅读:
    Uva673 Parentheses Balance
    cordforce Educational Codeforces Round 47 补题笔记 <未完>
    cordforce 495 补题 <未完>
    linux 基本命令笔记
    app审核相关
    CATransform3D
    UITableView点击切换状态分析
    iOS大转盘抽奖
    被忽视的控件UIToolbar
    AVPlayerViewController视频播放器
  • 原文地址:https://www.cnblogs.com/xiaobaibailongma/p/15114744.html
Copyright © 2011-2022 走看看