SpringBoot 后端接收前端传值的方法

1、通过HttpServletRequest接收,适用于GET 和 POST请求方式

      通过HttpServletRequest对象获取请求参数

@RestController
@RequestMapping("/tools")
public class InnerController {
    @RequestMapping("/addUser2")
    public String addUser2(HttpServletRequest request, HttpServletResponse response) {
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        System.out.println("username is:"+username);
        System.out.println("password is:"+password);
        return "success";
    }
}

 2、SpringMVC,可以不设置任何注解即可接收参数

  1.SpringMVC,可以不设置任何注解即可接收参数,比如 
   @GetMapping("/category")
   public String category( Long id) {
    System.out.println(id);
    return "post/category";
}
  可以通过  /category 访问 ,也可以通过 /category?id=1 访问
2.SpringMVC ,也可以自动包装成对象 
   url  /category?title=测试    或者  /category  都能访问到目标资源
 
    @GetMapping("/category")
    public String category( MPost post ) {
        System.out.println(post.getTitle());
        return "post/category";
    }

多条参数的话可以这样

 @GetMapping(value = "exportVisitorInfoList")
        public Book exportVisitorInfoList(String Code,String type,String startTime,String endTime){
                      Book book = new Book();
            book.setCompanyCode(Code);
            book.setType(type);
            book.setStartTime(startTime);
            book.setEndTime(endTime);
            return book;
        }

1.SpringMVC的自动封装(不传参也能进入)
@RequestParam(必须传参,但可以手动设置为false)
@PathVariable(符合设定的正则表达式才允许进入,而且不能为空)
2.对比可知,主要是为了url提供更加严格的限制,以防止一些其他url进入该action。
3.提供复杂的接受参数的方式@RequestBody ,但必须将参数放置在@RequestBody中
4.针对PathVariable 需要注意的是参数中包含特殊字符的问题,可能导致参数不全。
5.对于各种请求方式,验证一下当前用户,对url进行加密 是有必要的。(尤其是关键数据)

 

3、通过@PathVariable获取路径中的参数,适用于GET请求

   通过注解获取url参数

@RestController
@RequestMapping("/tools")
public class InnerController {
    @RequestMapping(value="/addUser4/{username}/{password}",method=RequestMethod.GET)
    public String addUser4(@PathVariable("username") String username,@PathVariable String password) {
        System.out.println("username is:"+username);
        System.out.println("password is:"+password);
        return "success";    }
}

测试代码

<script>
    var xhr = new XMLHttpRequest()
    xhr.open('GET', 'http://localhost:8080/tools/addUser4/username=zhangsan/password=123') // 设置请求行
    xhr.send() 
    xhr.onload=function(){
      if(xhr.readyState!==4) return 
      console.log(xhr.responseText)
    }
  </script>

自动将URL中模板变量{username}和{password}绑定到通过@PathVariable注解的同名参数上,即入参后username=zhangsan、password=123

 4、用注解@RequestParam绑定请求参数到方法入参,适用于GET 和 POST请求方式

     添加@RequestParam注解,默认会校验入参,如果请求不带入参则会报错,可以通过设置属性required=false解决,例如: @RequestParam(value="username", required=false) ,这样就不会校验入参,于第一种请求方式一样

@RestController
@RequestMapping("/tools")
public class InnerController {
    @RequestMapping(value="/addUser6",method=RequestMethod.GET)
    public String addUser6(@RequestParam("username") String username,@RequestParam("password") String password) {
        System.out.println("username is:"+username);
        System.out.println("password is:"+password);
        return "success";
    }
}

5、用注解@RequestBody绑定请求参数到方法入参 , 用于POST请求

@RequestBody主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的)

@RestController
@RequestMapping("/tools")
public class InnerController {
    @RequestMapping(value="/addUser7",method=RequestMethod.POST)
    public String addUser7(@RequestBody DemoUser user) {
        System.out.println("username is:"+user.getUsername());
        System.out.println("password is:"+user.getPassword());
        return "success";
    }
}

测试代码:

<script>
    var xhr = new XMLHttpRequest()
    xhr.open('POST', 'http://localhost:8080/tools/addUser7') // 设置请求行
    xhr.setRequestHeader('Content-Type','application/json')
    xhr.send('{"username":"zhangsan","password":"123"}') 
    xhr.onload=function(){
      if(xhr.readyState!==4) return 
      console.log(xhr.responseText)
    }
  </script>

 

posted @ 2021-10-15 10:08  迷糊桃  阅读(5268)  评论(0编辑  收藏  举报