SpringBoot获取http请求参数的方法

原文:https://www.cnblogs.com/zhanglijun/p/9403483.html

有七种Java后台获取前端传来参数的方法,稍微罗列一下。

1. 直接把表单里面的参数写进 Controller 相应方法的形参中去,这个获取参数的方法适合get提交,而不适合post提交。

	/**
* 1.直接把表单的参数写在Controller相应的方法的形参中
* @param username
* @param password
* @return
*/
@RequestMapping("/addUser")
public String addUser(String username,String password) {
System.out.println("username is:"+username);
System.out.println("password is:"+password);
return "demo/index";
}

url形式:http://localhost/jayvee/demo/addUser?username=wjw&password=123456 提交的参数名称必须和Controller方法中定义的参数名称一致。

2. 使用 HttpServletRequest 获取参数,适用于post和get方法。

	/**
* 2、通过HttpServletRequest接收
* @param request
* @return
*/
@RequestMapping("/addUser")
public String addUser(HttpServletRequest request) {
String username=request.getParameter("username");
String password=request.getParameter("password");
System.out.println("username is:"+username);
System.out.println("password is:"+password);
return "demo/index";
}

3. 通过建立一个bean来获取参数,适用于 post 和 get。

首先创建一个和表单对应的bean

package demo.model;
public class UserModel {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

使用创建的 bean 来封装接收到前端传来的参数

	/**
* 3、通过一个bean来接收
* @param user
* @return
*/
@RequestMapping("/addUser")
public String addUser(UserModel user) {
System.out.println("username is:"+user.getUsername());
System.out.println("password is:"+user.getPassword());
return "demo/index";
}

4. 通过 PathVariable 获取传进的参数。

	/**
* 4、通过@PathVariable获取路径中的参数
* @param username
* @param password
* @return
*/
@RequestMapping(value="/addUser/{username}/{password}",method=RequestMethod.GET)
public String addUser(@PathVariable String username,@PathVariable String password) {
System.out.println("username is:"+username);
System.out.println("password is:"+password);
return "demo/index";
}

例如,访问http://localhost/jayvee/demo/addUser4/wjw/123465时,则自动将URL中模板变量{username}和{password}绑定到通过@PathVariable注解的同名参数上,即入参后username=wjw、password=123465。

5. 使用@ModelAttribute注解获取POST请求的FORM表单数据

jsp表单

<form action ="<%=request.getContextPath()%>/demo/addUser5" method="post">
用户名:&nbsp;<input type="text" name="username"/><br/>
密&nbsp;&nbsp;码:&nbsp;<input type="password" name="password"/><br/>
<input type="submit" value="提交"/>
<input type="reset" value="重置"/>
</form>

Java Controller

	/**
* 5、使用@ModelAttribute注解获取POST请求的FORM表单数据
* @param user
* @return
*/
@RequestMapping(value="/addUser",method=RequestMethod.POST)
public String addUser(@ModelAttribute("user") UserModel user) {
System.out.println("username is:"+user.getUsername());
System.out.println("password is:"+user.getPassword());
return "demo/index";
}

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

当请求参数username不存在时会有异常发生,可以通过设置属性required=false解决,例如: @RequestParam(value="username", required=false)

/**
* 6、用注解@RequestParam绑定请求参数到方法入参
* @param username
* @param password
* @return
*/
@RequestMapping(value="/addUser",method=RequestMethod.GET)
public String addUser(@RequestParam("username") String username,@RequestParam("password") String password) {
System.out.println("username is:"+username);
System.out.println("password is:"+password);
return "demo/index";
}

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

/**
* 7、用注解@Requestbody绑定请求参数到方法入参
* @param username
* @param password
* @return
*/
@RequestMapping(value="/addUser",method=RequestMethod.POST)
public String addUser(@RequestBody UserDTO userDTO) {
System.out.println("username is:"+userDTO.getUserName());
System.out.println("password is:"+userDTO.getPassWord());
return "demo/index";
} //UserDTO 这个类为一个实体类,里面定义的属性与URL传过来的属性名一一对应。

以上为总结的七种后台springbooot获取前端传进参数的方法。

spring boot的@RequestParam和@RequestBody的区别

1、问题描述

由于项目是前后端分离,因此后台使用的是spring boot,做成微服务,只暴露接口。接口设计风格为restful的风格,在get请求下,后台接收参数的注解为RequestBody时会报错;在 post请求下,后台接收参数的注解为RequestParam时也会报错。

2、问题原因

由于spring的RequestParam注解接收的参数是来自于requestHeader中,即请求头,也就是在url中,格式为xxx?username=123&password=456,而RequestBody注解接收的参数则是来自于requestBody中,即请求体中。

3、解决方法

因此综上所述,如果为get请求时,后台接收参数的注解应该为RequestParam,如果为post请求时,则后台接收参数的注解就是为RequestBody。附上两个例子,截图如下:

4、get请求



5、post请求



另外,还有一种应用场景,接口规范为resultful风格时,举个例子:如果要获取某个id下此条问题答案的查询次数的话,则后台就需要动态获取参数, 其注解为@PathVariable,并且requestMapping中的value应为value="/{id}/queryNum",截图如下:

SpringBoot获取http请求参数的方法的更多相关文章

  1. springboot获取URL请求参数的几种方法

    原文地址:http://www.cnblogs.com/xiaoxi/p/5695783.html 1.直接把表单的参数写在Controller相应的方法的形参中,适用于get方式提交,不适用于pos ...

  2. springboot获取URL请求参数的多种方式

    1.直接把表单的参数写在Controller相应的方法的形参中,适用于get方式提交,不适用于post方式提交. /** * 1.直接把表单的参数写在Controller相应的方法的形参中 * @pa ...

  3. beego获取用户请求参数的方法

    我们经常需要获取用户传递的数据,包括 Get.POST 等方式的请求,beego 里面会自动解析这些数据,你可以通过如下方式获取数据: GetString(key string) string Get ...

  4. SpringBoot 拦截器获取http请求参数

    SpringBoot 拦截器获取http请求参数-- 所有骚操作基础 目录 SpringBoot 拦截器获取http请求参数-- 所有骚操作基础 获取http请求参数是一种刚需 定义拦截器获取请求 为 ...

  5. node.js获取请求参数的方法和文件上传

    var http=require('http') var url=require('url') var qs=require('querystring') http.createServer(onRe ...

  6. Spring MVC(三)控制器获取页面请求参数以及将控制器数据传递给页面和实现重定向的方式

    首先做好环境配置 在mvc.xml里进行配置 1.开启组件扫描 2.开启基于mvc的标注 3.配置试图处理器 <?xml version="1.0" encoding=&qu ...

  7. express 获取post 请求参数

    在 Express 中没有内置获取表单 POST 请求体的 API , 我们需要添加第三方插件库 安装: npm install --save body-parser 配置: var bodyPars ...

  8. Javascript 获取链接(url)参数的方法

    有时我们需要在客户端获取链接参数,一个常见的方法是将链接当做字符串,按照链接的格式分解,然后获取对应的参数值.本文给出的就是这个流程的具体实现方法. 当然,我们也可以用正则直接匹配,文章中也给出了一个 ...

  9. 使用@RequestParam绑定请求参数到方法参数

    @RequestParam注解用于在控制器中绑定请求参数到方法参数.用法如下:@RequestMapping public void advancedSearch(   @RequestParam(& ...

随机推荐

  1. HW - VCN 介绍

    VCN 是个管理摄像机的平台 用来增删改查摄像机,获取摄像机视频流,获取录像 vcn会基于我们的接口做一次开发,作为相机的统一管理入口,获取相机的信息

  2. python3练习100题——004

    继续做题-经过python3的测试 原题链接:http://www.runoob.com/python/python-exercise-example4.html 题目:输入某年某月某日,判断这一天是 ...

  3. Codeforces 1304E. 1-Trees and Queries 代码(LCA 树上两点距离判奇偶)

    https://codeforces.com/contest/1304/problem/E #include<bits/stdc++.h> using namespace std; typ ...

  4. ECMAScript基本语法——⑤运算符 三元运算符

    ?: 简化ifelse的操作

  5. 在ASP.NET 中调用 WebService 服务

    一.webservice定义 详见 https://www.cnblogs.com/phoebes/p/8029464.html 二.在ASP.NET MVC 中调用 webservice 1:要调用 ...

  6. linux 配置compoer

    配置默认php 删除 rm -f /usr/bin/php 改到php7.3版本的composer /bin/php /usr/bin/php 多版本支持 配置php7专用composer70 cd ...

  7. 题解【AcWing91】最短Hamilton路径

    题面 看到数据范围这么小,第一眼想到爆搜. 然而这样做的复杂度是 \(\mathcal{O}(n! \times n)\) 的,明显会 TLE. 于是考虑状压 DP. 我们设 \(dp_{i,j}\) ...

  8. new Vue发生了什么(五)

    从入口代码开始分析,我们先来分析 new Vue 背后发生了哪些事情.我们都知道,new 关键字在 Javascript 语言中代表实例化是一个对象,而 Vue 实际上是一个类,类在 Javascri ...

  9. 傻傻分不清之 Cookie、Session、Token、JWT

    傻傻分不清之 Cookie.Session.Token.JWT 什么是认证(Authentication) 通俗地讲就是验证当前用户的身份,证明“你是你自己”(比如:你每天上下班打卡,都需要通过指纹打 ...

  10. 题解 【Codeforces988E】Divisibility by 25

    本题是数论好题! 首先我们需要了解一个关于数论的性质: 一个数只要后两位能被25(或4)整除,这个数就能被25(或4)整除. 同理,后三位:(或8).后四位:(或16)亦是如此. 所以,我们只需要判断 ...