当前位置:网站首页>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);
}
边栏推荐
- Unity2D 动画器无法 创建过渡
- leetcode-Array
- Learn about spark project on nebulagraph
- Sequential search, half search, block search~
- 漫谈软件缺陷管理的实践
- 时序动作定位 | 用于弱监督时态动作定位的细粒度时态对比学习(CVPR 2022)
- Xiao He shows his sharp corners and says hello to flutter app
- Jincang database kingbasees SQL language reference manual (5. Operators)
- Embedded sharing collection 15
- 二叉树的性质 ~
猜你喜欢

Kingbasees SQL language reference manual of Jincang database (10. Query and sub query)

移动web

Traversal of the first, middle, and last order of a binary tree -- Essence (each node is a "root" node)

Easycvr video square channel display and video access full screen display style problem repair

2022年下半年系统集成项目管理工程师(软考中级)报名条件

Ros2 preliminary: basic communication with topic

102. (cesium chapter) cesium road streamer

分布式 | 实战:将业务从 MyCAT 平滑迁移到 dble

Day110. Shangyitong: gateway integration, hospital scheduling management: Department list, statistics based on date, scheduling details

二叉树的前中后序遍历——本质(每个节点都是“根”节点)
随机推荐
ERROR: Could not open requirements file: [Errno 2] No such file or directory: ‘requirments.txt’
What is spark serialization for?
ament_ Cmake generates the ros2 library and links it
Kingbasees SQL language reference manual of Jincang database (8. Function (10))
L. Link with Level Editor I dp
二叉树的性质 ~
时序动作定位 | 用于弱监督时态动作定位的细粒度时态对比学习(CVPR 2022)
Niuke network: TOPK problem of additive sum between two ordinal groups
[SQL optimization] (big table tips) sometimes a 2-hour SQL operation may take only 1 minute
Flex layout
Kingbasees SQL language reference manual of Jincang database (11. SQL statement: abort to alter index)
Can you make a JS to get the verification code?
[2023 Jerry technology approval test questions in advance] ~ questions and reference answers
Print linked list in reverse order
Traversal of the first, middle, and last order of a binary tree -- Essence (each node is a "root" node)
Day110. Shangyitong: gateway integration, hospital scheduling management: Department list, statistics based on date, scheduling details
Detailed explanation of the whole process of coding's pressure measurement operation
The time complexity of two recursive entries in a recursive function
[free and easy to use] holiday query interface
Kingbasees SQL language reference manual of Jincang database (6. Expression)