当前位置:网站首页>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);
}
边栏推荐
- [MySQL must know and know] time function number function string function condition judgment
- Solve vagrant's error b:48:in `join ': incompatible character encodings: GBK and UTF-8 (encoding:: Compatib
- K. Link with Bracket Sequence I dp
- Taobao JD pinduoduo Tiktok taote 1688 and other multi platform commodity app details API interfaces (commodity details page data interface, commodity sales interface, keyword search commodity sales in
- [MySQL from introduction to proficiency] [advanced chapter] (VI) storage engine of MySQL tables, comparison between InnoDB and MyISAM
- Understanding the mathematical essence of machine learning
- 递归处理——子问题
- Talking about the practice of software defect management
- [2023 Jerry technology approval test questions in advance] ~ questions and reference answers
- Solution to slow download speed of vagrant
猜你喜欢

Jdbc流式查询与游标查询

How to divide the disks under the devices and drives in win10 new computer

Kingbasees SQL language reference manual of Jincang database (7. Conditional expression)

Leetcode:741. picking cherries

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

满二叉树 / 真二叉树 / 完全二叉树 ~

Operating steps for uninstalling the mobile app

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

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

Print linked list in reverse order
随机推荐
Kingbasees SQL language reference manual of Jincang database (7. Conditional expression)
VRRP protocol and experimental configuration
Detailed explanation of the whole process of coding's pressure measurement operation
Workflow activiti5.13 learning notes (I)
Six sixths -- it's a little late and a little shallow
Kingbasees SQL language reference manual of Jincang database (9. Common DDL clauses)
Establishment of log collection and analysis platform-1-environment preparation
Solution to slow download speed of vagrant
K. Link with Bracket Sequence I dp
Leetcode:741. picking cherries
Operating steps for uninstalling the mobile app
金仓数据库 KingbaseES SQL 语言参考手册 (8. 函数(十一))
Blurring of unity pixel painting
Servlet filter details
5-year-old Test Engineer - how to choose the next step?
递归函数中 有两个递归入口的时间复杂度
Kingbasees SQL language reference manual of Jincang database (8. Function (10))
H. Take the Elevator 贪心
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
Ros2 preliminary: basic communication with topic