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. LED Candle Light Factory-LED Candle Light: Full Of Romance

    LED candle lights are beautiful and can add elegance and romance to any space. These candles make th ...

  2. Java如何自定义注解

    本文主要是记录所学,以供后续参考.注解是Java 1.5引入的,Java自定义注解是通过运行时靠反射获取注解,注解相当于是一种嵌入在程序中的元数据,可以使用注解解析工具或编译器对其进行解析,也可以指定 ...

  3. ContestHunter 1201 最大子序和

    描述 输入一个长度为n的整数序列,从中找出一段不超过m的连续子序列,使得整个序列的和最大. 例如 1,-3,5,1,-2,3 当m=4时,S=5+1-2+3=7当m=2或m=3时,S=5+1=6 输入 ...

  4. IP地址分类及其相关计算问题

    IP地址分类及其相关计算问题 公网IP和子网IP 公网IP: • A类:1.0.0.0 到 127.255.255.255 主要分配 给大量主机而局域网网络数量较少的大型网络 • B类:128.0.0 ...

  5. SSH、telnet配置以及它们之间区别

    命令: SSH ip domain-name www.baidu.com --配置主机名(用来远程访问) user privilege secret --配置账户名和密码 line vty --配置端 ...

  6. 使用yum安装报错:[Errno 256] No more mirrors to try

    背景:我使用yum方式安装软件时,比如zabbix这种软件,我们在安装时一般都是直接到zabbix官网,按照官方的步骤进行安装,但是有一个问题,官方的服务器不在国内,时常会在安装时导致超时报错.此时解 ...

  7. PP: A dual-stage attention-based recurrent neural network for time series prediction

    Problem: time series prediction The nonlinear autoregressive exogenous model: The Nonlinear autoregr ...

  8. docker远程访问

    查看版本 docker version 查看信息 docker info 修改配置文件 ubuntu在 /etc/default/docker centos在/usr/lib/systemd/syst ...

  9. 洛谷P1603 斯诺登的密码

    https://www.luogu.org/problem/P1603 #include<bits/stdc++.h> using namespace std; struct s { st ...

  10. Python中super的用法【转载】

    Python中super的用法[转载] 转载dxk_093812 最后发布于2019-02-17 20:12:18 阅读数 1143  收藏 展开 转载自 Python面向对象中super用法与MRO ...