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、通过@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
3、用注解@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";
}
}
4、用注解@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>
参考于:https://www.cnblogs.com/unknows/p/11276345.html
SpringBoot 后端接收前端传值的方法的更多相关文章
- PHP接收前端传值各种情况整理
PHP接收前端传值各种情况整理 服务端代码: header('Access-Control-Allow-Origin:*'); var_dump($_POST); exit; 情况 1) 传null ...
- Vue+SpringBoot后端接收包含单属性和List数组的json对象
这次主要是针对springboot后台接收的json中包含多对象(如List数组/单属性)所写的一篇文章.虽然网上类似情况很多,尝试了一个晚上,都没有解决问题,最后还是在师兄的帮助下完美解决. vue ...
- SpringMVC接收前端传值有哪些方式?
有很多种,比如: 1.通过@RequestParam注解接收请求参数: 2.通过Bean封装,接收多个请求参数 3.通过@ModelAttribute绑定接收前端表单数据 4.通过@PathVaria ...
- spring MVC 后端 接收 前端 批量添加的数据(简单示例)
第一种方式:(使用ajax的方式) 前端代码: <%@ page contentType="text/html;charset=UTF-8" language="j ...
- C#后端接收前端的各种类型数据
前端往后端提交数据的方式常用的就这么三种:1.form提交:2.url参数提交:3.json提交 1.针对表单form方式的提交 在后端使用Request.Form的方式接收,比如 前端代码片段: v ...
- java后端接收前端传来的复杂对象(包含List对象集合)
最近在和安卓对接口的时候发现往java后端传数据的时候,后台对象无法接收. 说明:后台对象为 类似结构 ObjectA{ private String a; private String b; pr ...
- Ajax前后端交互——后端接收前端页面变量
核心代码: app.py from flask import Flask, render_template, request, jsonify app = Flask(__name__) @app.r ...
- SpringBoot接收前端参数的三种方法
都是以前的笔记了,有时间就整理出来了,SpringBoot接收前端参数的三种方法,首先第一种代码: @RestController public class ControllerTest { //访问 ...
- 关于mui前端传值,springboot后台接收值的问题
最近做app,使用mui的ajax给后台传参,后台一直接收不到值,表示很蛋疼.这里通过网上搜索加上个人实践,总结归纳了三种前端传值和后台接收的方式. 第一种: 前端: data: JSON.strin ...
随机推荐
- CSS样式下border的几种线型
在用border的时候经常会忘记它有多少种线型以及各种线型的写法:每次都得从头开始,或是用到Google.百度之类的,有空整理了一下 (1)none (没有边框,无论边框宽度设为多大) (2)dott ...
- How to check type of files without extensions in python? 不通过文件扩展名,怎样知道文件类型?
有一个命令 file 可以用 $ file fuck fuck.png: PNG image data, 1122 x 750, 8-bit colormap, non-interlaced pyth ...
- 查看所有日志命令:journalctl
journalctl命令作用:实时查看所有日志(内核日志和应用日志) 语法格式: journalctl [参数] 常用参数:-k 查看内核日志-b 查看系统本次启动的日志-u 查看指定服务的日志-n ...
- Pytest系列(12)- 测试结果生成HTML报告插件之pytest-html的详细使用
如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 环境前提 Python3.6+ 安 ...
- Linux学习笔记--快捷键
桌面 ALT+空格 打开窗口菜单 ALT+F1 聚焦到桌面左侧任务导航栏,可按上下键导航 ALT+F2 运行命令 ALT+F4 关闭窗口 ALT+TAB 切换程序窗口 PRINT 桌面截图 S ...
- 洛谷P1056——排座椅(模拟,贪心,排序)
https://www.luogu.org/problem/show?pid=1056 题目描述 上课的时候总会有一些同学和前后左右的人交头接耳,这是令小学班主任十分头疼的一件事情.不过,班主任小雪发 ...
- scrum项目冲刺_day01总结
摘要:今日完成任务. 1.app基本框架页面正在进行 2.图像识别正在进行 总任务: 一.appUI页面 二.首页功能: 1.图像识别功能 2.语音识别功能 3.垃圾搜索功能 4.相关新闻爬取 三.我 ...
- Python程序调用摄像头实现人脸识别
使用简单代码实现摄像头进行在线人脸识别 import cv2 import sys import logging as log import datetime as dt from time impo ...
- Markdown公式用法大全
目录 基本语法 两种代码引用方式 插入链接并描述 插入图片 有序列表 无序列表 分割线 表格 如何插入公式 如何输入上下标 如何输入括号和分隔符 如何输入分数 如何输入开方 如何输入省略号 如何输入矢 ...
- 哪5种IO模型?什么是select/poll/epoll?同步异步阻塞非阻塞有啥区别?全在这讲明白了!
系统中有哪5种IO模型?什么是 select/poll/epoll?同步异步阻塞非阻塞有啥区别? 本文地址http://yangjianyong.cn/?p=84转载无需经过作者本人授权 先解开第一个 ...