当前位置:首页 > java > 正文内容

springboot 服务端获取前端传过来的参数7种方式

关中浪子3年前 (2021-11-08)java1161
【腾讯云】2核2G4M云服务器新老同享99元/年,续费同价
找梯子最重要的就是稳定,这个已经上线三年了,一直稳定没有被封过,赶紧下载备用吧!

1、直接把表单的参数写在Controller相应的方法的形参中,适用于GET 和 POST请求方式

"/tools" "/addUser1""username is:"+"password is:"+ "success"

测试代码

POST请求方式
  <script>     xhr = 'POST', 'http://localhost:8080/tools/addUser1') 
    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded''username=zhangsan&password=123') 
    xhr.onload=(xhr.readyState!==4)  </script>
GET请求方式: 
 <script>     xhr = 'GET', 'http://localhost:8080/tools/addUser1?username=zhangsan&password=123') 
    xhr.send() 
    xhr.onload=(xhr.readyState!==4)  </script>

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

"/tools" "/addUser2"=request.getParameter("username"=request.getParameter("password""username is:"+"password is:"+ "success"

测试代码同上

3、通过一个bean来接收,适用于GET 和 POST请求方式
(1)建立一个和表单中参数对应的bean

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructorpublic class DemoUser {    private String username;    private String password;
}

(2)用这个bean来封装接收的参数

"/tools" "/addUser3""username is:"+"password is:"+ "success"

测试代码同上

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

"/tools" ="/addUser4/{username}/{password}",method="username is:"+"password is:"+ "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

5、使用@ModelAttribute注解获取参数,适用于POST请求

@RestController
@RequestMapping("/tools")public class InnerController {
    @RequestMapping(value="/addUser5",method=RequestMethod.POST)    public String addUser5(@ModelAttribute("user") 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/addUser5') // 设置请求行
    xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded')
    xhr.send('username=zhangsan&password=123') 
    xhr.onload=function(){      if(xhr.readyState!==4) return 
      console.log(xhr.responseText)
    }  </script>

6、用注解@RequestParam绑定请求参数到方法入参

当请求参数username不存在时会有异常发生,可以通过设置属性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";
    }
}

测试代码同上

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

@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";
    }
}

测试代码:    请求头需要指定为json类型

  <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>
DemoUser这个类为一个实体类,里面定义的属性与URL传过来的属性名一一对应。


扫描二维码推送至手机访问。

版权声明:本文由码农翻生发布,如需转载请注明出处。

本文链接:https://lubojian.cn/post/57.html

分享给朋友:

相关文章

SpringBoot   http请求 提交日期参数 转换失败的解决方法 (无法自动转换字符串到日期)

SpringBoot http请求 提交日期参数 转换失败的解决方法 (无法自动转换字符串到日期)

如果你有类似如下报错信息,可阅读此文尝试解决:2018-08-18 14:05:17.687  WARN 17100 --- [p-nio-80-exec-2] .w.s...

springboot  导出数据到 excel

springboot 导出数据到 excel

问题来源:前一段时间公司的项目有个导出数据的需求,要求能够实现全部导出也可以多选批量导出(虽然不是我负责的,我自己研究了研究),我们的项目是xboot前后端分离系统,后端的核心为SpringBoot 2.2.6.RELEASE,因此今天我主...

SQL之CASE WHEN用法详解

当我们需要从数据源上 直接判断数据显示代表的含义的时候 ,就可以在SQL语句中使用 Case When这个函数了.Case具有两种格式。简单Case函数和Case搜索函数。 第一种 格式 : 简单Case函数 :格式说明...

java poi读取excel文件(xlsx)

poi读取excel文件先maven导入jar包<dependency>     <groupId>org.apache.poi</groupId>  ...

java使用poi读取excel表格

java使用poi读取excel表格

java读取excel中包括图片、日期、字符串格式。导入依赖        <dependency>    &nbs...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。