SpringBoot:前端提交数据,服务端无法获取数据

复制代码
http://www.xxx.com?phone=111111111111&code=1332

上述访问传值方式为键值对方式,服务端springmvc获取 >>

复制代码
// 在HttpServlet实现类的doGet、doPost方法中获取前端传来的值
doGet(ServerHttpRequest request){
    String phone = request.getParameter("phone");
}

// controller 中获取
saveInfo(String phone){
    System.out.println(phone)
}

如果在springcloud网关中集中处理,获取值如下

复制代码
public class ValidateCodeGatewayFilter extends AbstractGatewayFilterFactory {

	@Override
	public GatewayFilter apply(Object config) {
		return (exchange, chain) -> {

			ServerHttpRequest request = exchange.getRequest();
          	String mobile = request.getQueryParams().getFirst("mobile");
            String code = request.getQueryParams().getFirst("code");

			return chain.filter(exchange);
		};
	}
}

如果传输的值为 body如下图 >>

这个时候我们服务端获取值的时候需要添加*@RequestBody,@RequestBody*主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的);而最常用的使用请求体传参的无疑是POST请求了

相关推荐
zh_xuan1 小时前
c++ jthread
开发语言·c++·jthread
旧梦95271 小时前
Java 捕获多个异常:从基础语法到最佳实践
java·开发语言
2601_962189602 小时前
【SpringBoot】使用IDEA创建SpringBoot项目
java·spring boot·intellij-idea
步行cgn2 小时前
Spring Boot 启动报错:MissingServletWebServerFactoryBean 的排查与解决
java·spring boot·后端
XLYcmy2 小时前
HTML/CSS/JS 基础与 Vue 技术栈深度解析
java·前端·css·html·vue3·vue2·api
~木雨2 小时前
String 底层原理与常量池全解析:byte []+coder、intern 陷阱、substring 内存泄漏,一篇讲透
java·jvm·内存优化·string·字符串常量池
新知图书2 小时前
12.3 智能体中Skill的约束与自进化实战
java·前端·数据库
风月说与山鬼2 小时前
四、Tailwind CSS——间距与尺寸
前端·css·tailwind css
en.en..2 小时前
C语言 static函数与头文件封装规范
java·c语言·前端
海兰2 小时前
【 Python 量化交易】开篇
开发语言·python·信息可视化·量化交易