当前位置:网站首页>Servlet无法直接获取request请求中的JSON格式数据
Servlet无法直接获取request请求中的JSON格式数据
2022-07-26 06:08:00 【梦凝哲雪】
Servlet无法直接获取request请求中的JSON格式数据
Reuqest 的 常规方法
request.getParameter(name)
一般情况下的
- 表单请求 和 表单序列化
- ajax.post( url, params, data=>{} )
- axios.post( url, qs.stringify{ (params}).then( res=>{ })
- …
现在 JSON格式 数据 就无法获取参数了
let params = {
"uname":"yc", "upwd":"123321"}
$.ajax({
type:"post",
url:"amdin/login",
contentType:'application/json;charset=UTF-8',
data: params , // 数据格式 json字符串传递
success:function(data){
console.log(data);
}
});
String uname = request.getParameter("uname"); // null
方案一
jQuery的ajax请求参数类型换成 默认的编码方式 键值对 K->V 序列化处理
contentType: "application/x-www-form-urlencoded",
方案二
借助三方库GSON 不但可以获取参数,而且还能封装到实体类中
/** * 前提: 在前台传json数据给后台的doPOST接口时,request的常规方案 拿不到json数据 显示 null * 利用三方库 对前端JSON数据格式的请求参数 封装成对象 * @param <T> * @param cls * @param request * @return * @throws IOException */
public static <T> T getJSONParamesToObject(Class<T> cls , HttpServletRequest request) throws IOException{
BufferedReader reader = request.getReader();
Gson gson = new Gson();
return gson.fromJson( reader, cls);
}
边栏推荐
- Traversal of the first, middle, and last order of a binary tree -- Essence (each node is a "root" node)
- 【Oracle SQL】计算同比与环比(列转行进行偏移)
- 金仓数据库 KingbaseES SQL 语言参考手册 (9. 常见DDL子句)
- [the most complete and detailed] ten thousand words explanation: activiti workflow engine
- WebAPI整理
- Qu Weihai, chairman and CEO of Xinyi interactive, adheres to mutual benefit and win-win results, and Qu Weihai promotes enterprise development
- Docking wechat payment (II) unified order API
- Meiker Studio - Huawei 14 day Hongmeng equipment development practical notes 4
- 金仓数据库 KingbaseES SQL 语言参考手册 (6. 表达式)
- How can red star Macalline design cloud upgrade the traditional home furnishing industry in ten minutes to produce film and television level interior design effects
猜你喜欢

Kingbasees SQL language reference manual of Jincang database (8. Functions (XI))

Meiker Studio - Huawei 14 day Hongmeng equipment development practical notes 4

Database SQL language practice

Matlab 向量与矩阵
C language explanation series - comprehensive exercises, guessing numbers games

vagrant下载速度慢的解决方法

flex布局
![[(SV & UVM) knowledge points encountered in written interview] ~ phase mechanism](/img/19/32206eb6490c2a5a7a8e746b5003c1.png)
[(SV & UVM) knowledge points encountered in written interview] ~ phase mechanism

VRRP protocol and experimental configuration

Using dynamic libraries in VS
随机推荐
ERROR: Could not open requirements file: [Errno 2] No such file or directory: ‘requirments.txt’
Viewing the technology stack of distributed system from the crash report of station B
[the most complete and detailed] ten thousand words explanation: activiti workflow engine
Ros2 preliminary: basic communication with topic
【2023杰理科技提前批笔试题】~ 题目及参考答案
招标信息获取
Day110. Shangyitong: gateway integration, hospital scheduling management: Department list, statistics based on date, scheduling details
2022年下半年系统集成项目管理工程师(软考中级)报名条件
A company installs monitoring for each station: write code only to see employees?
二叉排序树(BST) ~
Ganglia安装部署流程
对接微信支付(二)统一下单API
[MySQL from introduction to proficiency] [advanced chapter] (VI) storage engine of MySQL tables, comparison between InnoDB and MyISAM
CV (1)- Introduction
L. Link with Level Editor I dp
Database SQL language practice
金仓数据库 KingbaseES SQL 语言参考手册 (8. 函数(十一))
How can programmers improve mental internal friction?
[paper notes] anti packet loss joint coding for network speech steganography
How can red star Macalline design cloud upgrade the traditional home furnishing industry in ten minutes to produce film and television level interior design effects