SpringBoot POST請求接收多個參數(shù)值為null問題
問題描述
SpringBoot接口使用Post請求接收前端的JSON數(shù)據(jù)寫了多個參數(shù)
無論怎么寫獲取到的數(shù)據(jù)都是null
$.ajax({
url: "login4",
param: JSON.stringify({
"name": "name",
"age": 10
}),
type: "post",
dataType: "json",
success: function (data) {
},
error: function (data) {
}
}); @PostMapping("login4")
public void login4(String name, Integer age){
System.out.println("login3");
System.out.println(name + " -- " + age);
}原因分析
POST請求接收J(rèn)SON數(shù)據(jù)時使用簡單類型(Integer、String)等不能自動填充數(shù)據(jù)
必須要封裝成實(shí)體類或者使用Map接收
而且使用Map接收時還要注明泛型
解決方案
類似于以下這種方式
封裝一個實(shí)體類(實(shí)體類的屬性名要與前端請求參數(shù)對應(yīng))或使用Map<String,Object>
前面加上RequestBody注解就能獲取到了
@RequestMapping("/saveGrAuditLog")
public void auditUploads(@RequestBody AuditRequestEntity auditRequestEntity, HttpServletRequest request){
WriteLogParam writeLogParam = new WriteLogParam();
writeLogParam.setNote(auditRequestEntity.getNote());
writeLogParam.setOpterationType(auditRequestEntity.getOpterationType());
writeLogParam.setOptName(auditRequestEntity.getOptName());
writeLogParam.setResourceId(auditRequestEntity.getResourceId());
writeLogParam.setResourceName(auditRequestEntity.getResourceName());
log.info("******params >> "+ JSONUtil.toJsonStr(writeLogParam));
AuditLogRequestTwo.addAuditNew(request,auditRequestEntity.getParams(),auditRequestEntity.getOptUrl(),auditRequestEntity.getResult(),writeLogParam);
}
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決springboot configuration processor對maven子模塊不起作用的問題
這篇文章主要介紹了解決springboot configuration processor對maven子模塊不起作用的問題,本文通過圖文實(shí)例代碼給大家講解的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09
SpringBoot3.0集成MybatisPlus的實(shí)現(xiàn)方法
本文主要介紹了SpringBoot3.0集成MybatisPlus的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-08-08
SSL證書部署+SpringBoot實(shí)現(xiàn)HTTPS安全訪問的操作方法
文章介紹了SSL和HTTPS的工作原理,包括握手階段和安全數(shù)據(jù)傳輸階段,通過模擬HTTPS請求,展示了如何生成自簽名證書并配置Spring Boot應(yīng)用程序以支持HTTPS,總結(jié)指出,SSL和HTTPS對于保護(hù)網(wǎng)絡(luò)安全至關(guān)重要,感興趣的朋友一起看看吧2025-02-02

