当前位置:网站首页>Servlet cannot directly obtain JSON format data in request request
Servlet cannot directly obtain JSON format data in request request
2022-07-26 06:12:00 【Dream condenses zhe Xue】
Servlet Can't get... Directly request In the request JSON Format data
Reuqest Of Conventional methods
request.getParameter(name)
In general
- Form request and Form serialization
- ajax.post( url, params, data=>{} )
- axios.post( url, qs.stringify{ (params}).then( res=>{ })
- …
Now? JSON Format data You can't get parameters
let params = {
"uname":"yc", "upwd":"123321"}
$.ajax({
type:"post",
url:"amdin/login",
contentType:'application/json;charset=UTF-8',
data: params , // data format json String passing
success:function(data){
console.log(data);
}
});
String uname = request.getParameter("uname"); // null
Scheme 1
jQuery Of ajax Change the request parameter type to Default encoding Key value pair K->V Serialization
contentType: "application/x-www-form-urlencoded",
Option two
With the help of tripartite Library GSON Not only can we get parameters , And it can also be encapsulated into entity classes
/** * Premise : Pass it on the front desk json Data to the backstage doPOST Interface ,request The regular program of Can't get json data Show null * Use the third-party library On the front JSON Request parameters of data format Encapsulated as an object * @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);
}
边栏推荐
- How to divide the disks under the devices and drives in win10 new computer
- 分布式 | 实战:将业务从 MyCAT 平滑迁移到 dble
- Kingbasees SQL language reference manual of Jincang database (6. Expression)
- 2022年下半年系统集成项目管理工程师(软考中级)报名条件
- Optical quantum milestone: 3854 variable problems solved in 6 minutes
- CV (1)- Introduction
- Niuke network: TOPK problem of additive sum between two ordinal groups
- Introduction to three feasible schemes of grammatical generalization
- Servlet filter details
- K. Link with Bracket Sequence I dp
猜你喜欢

Knowledge precipitation I: what does an architect do? What problems have been solved
![[day04_0421] C language multiple choice questions](/img/18/42924b5994b385a3cf132742141d88.png)
[day04_0421] C language multiple choice questions

Etcd database source code analysis - cluster membership changes log

逆序打印链表

【Day05_0422】C语言选择题

Interview difficulties: difficulties in implementing distributed session, this is enough!

Mysql45 talking about global lock, table lock and row lock

Acquisition of bidding information

Kingbasees SQL language reference manual of Jincang database (8. Function (10))

Convolutional neural network (II) - deep convolutional network: case study
随机推荐
Practice operation and maintenance knowledge accumulation
英语句式参考纯享版 - 定语从句
1.12 basis of Web Development
K. Link with Bracket Sequence I dp
Flex layout
CV (1)- Introduction
Mysql45 talks about transaction isolation: why can't I see it after you change it?
Unity2d animator cannot create transition
Taobao pinduoduo Tiktok 1688 Suning taote jd.com and other keyword search commodity API interfaces (keyword search commodity API interface, keyword search commodity list interface, classification ID s
"Recursive processing of subproblems" -- judging whether two trees are the same tree -- and the subtree of another tree
Interview difficulties: difficulties in implementing distributed session, this is enough!
Matlab 向量与矩阵
K. Link with Bracket Sequence I dp
How to divide the disks under the devices and drives in win10 new computer
[day04_0421] C language multiple choice questions
Code Runner for VS Code,下载量突破 4000 万!支持超过50种语言
Niuke network: TOPK problem of additive sum between two ordinal groups
Viewing the technology stack of distributed system from the crash report of station B
VRRP principle and basic commands
Jz36 binary search tree and bidirectional linked list