当前位置:网站首页>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);
}
边栏推荐
- Leetcode:940. How many subsequences have different literal values
- H. Take the elevator greedy
- Flex layout
- Optical quantum milestone: 3854 variable problems solved in 6 minutes
- Two auxiliary functions of integral Mall for business user operation
- Leetcode:336. palindrome pair
- Implementation of PHP multitask second timer
- 顺序查找,折半查找,分块查找 ~
- Mysql45 talks about transaction isolation: why can't I see it after you change it?
- Kingbasees SQL language reference manual of Jincang database (11. SQL statement: abort to alter index)
猜你喜欢

Select sort / insert sort / bubble sort

1.12 Web开发基础

Interview questions for software testing is a collection of interview questions for senior test engineers, which is exclusive to the whole network

unity 像素画导入模糊问题

redis 哨兵集群搭建
![[Oracle SQL] calculate year-on-year and month on month (column to row offset)](/img/ee/59d050e03c2a4ba04de57df1322283.png)
[Oracle SQL] calculate year-on-year and month on month (column to row offset)
![[MySQL from introduction to proficiency] [advanced chapter] (VI) storage engine of MySQL tables, comparison between InnoDB and MyISAM](/img/19/ca3a5710ead3c5b9a222a8ae4ecb37.png)
[MySQL from introduction to proficiency] [advanced chapter] (VI) storage engine of MySQL tables, comparison between InnoDB and MyISAM

Modifiers should be declared in the correct order 修饰符应按正确的顺序声明

Introduction to three feasible schemes of grammatical generalization

Unity2d animator cannot create transition
随机推荐
Kingbasees SQL language reference manual of Jincang database (10. Query and sub query)
Matlab vector and matrix
Leetcode:336. palindrome pair
C language explanation series - comprehensive exercises, guessing numbers games
分布式 | 实战:将业务从 MyCAT 平滑迁移到 dble
Day110. Shangyitong: gateway integration, hospital scheduling management: Department list, statistics based on date, scheduling details
VS中使用动态库
Kingbasees SQL language reference manual of Jincang database (7. Conditional expression)
WebAPI整理
[2023 Jerry technology approval test questions in advance] ~ questions and reference answers
What is spark serialization for?
招标信息获取
“子问题的递归处理”——判断两棵树是不是相同的树——以及 另一颗树的子树
Unity2D 动画器无法 创建过渡
1.12 Web开发基础
Six sixths -- it's a little late and a little shallow
时序动作定位 | 用于弱监督时态动作定位的细粒度时态对比学习(CVPR 2022)
Day110.尚医通:Gateway集成、医院排班管理:科室列表、根据日期统计数据、排班详情
[(SV & UVM) knowledge points encountered in written interview] ~ phase mechanism
H. Take the Elevator 贪心