springboot @RequestBody 接收字符串實例
springboot @RequestBody 接收字符串
- springboot 2.1.1.RELEASE
@RequestBody 接收字符串
@RequestMapping(method = {RequestMethod.POST}) public ResultEntity form1(@RequestBody String requestBody) throws UnsupportedEncodingException { logger.info("================ request body ================");\ logger.info("request body is : {}", requestBody); }
向接口傳送 application/json 格式的數(shù)據(jù)
客戶端調(diào)用代碼如下:
$.ajax({ url:'http://localhost/api/spd', data: JSON.stringify({name:'zhangsan', age: 18}), type:'POST', contentType: 'application/json', success:function(result){ console.log(result); }, error:function(error){ console.log(error); } });
服務(wù)端執(zhí)行結(jié)果:
00:11:55.972 [http-nio-8020-exec-5] INFO c.c.api.SpdApi - [form1,45] - request body is : {"name":"zhangsan","age":18}
向接口傳送 text/plain 格式的數(shù)據(jù)
客戶端調(diào)用代碼如下:
$.ajax({ url:'http://localhost/api/spd', data: 'this is a message', type:'POST', contentType: 'text/plain', success:function(result){ console.log(result); }, error:function(error){ console.log(error); } });
服務(wù)端執(zhí)行結(jié)果:
23:46:04.953 [http-nio-8020-exec-1] INFO c.c.api.SpdApi - [form1,45] - request body is : 'this is a message'
替代 @RequestBody 的辦法
如果不想用 @RequestBody ,可以使用下面的方法:
protected String getRequestBody(HttpServletRequest request) { try { BufferedReader reader = request.getReader(); char[] buf = new char[512]; int len = 0; StringBuffer contentBuffer = new StringBuffer(); while ((len = reader.read(buf)) != -1) { contentBuffer.append(buf, 0, len); } return contentBuffer.toString(); } catch (IOException e) { e.printStackTrace(); } return "null"; }
@RequestBody接收前端傳來的json值為空
這個真的很腦抽。。。
我忘了在函數(shù)接收處寫@RequestBody,至于其他博主說需要在BO包中加@JsonProperty(value = "xxx"),
或者什么駝峰命名法,也許是版本原因,沒有這個必要,emmm,檢查自己的函數(shù)接收參數(shù)叭
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
jasypt SaltGenerator接口定義方法源碼解讀
這篇文章主要為大家介紹了jasypt SaltGenerator接口定義方法源碼解讀,,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09Springboot設(shè)置默認(rèn)訪問路徑方法實現(xiàn)
這篇文章主要介紹了Springboot設(shè)置默認(rèn)訪問路徑方法實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12springboot2.0整合logback日志的詳細(xì)代碼
這篇文章主要介紹了springboot2.0整合logback日志的應(yīng)用場景分析,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-02-02使用Spring?Boot如何限制在一分鐘內(nèi)某個IP只能訪問10次
有些時候,為了防止我們上線的網(wǎng)站被攻擊,或者被刷取流量,我們會對某一個ip進(jìn)行限制處理,這篇文章,我們將通過Spring?Boot編寫一個小案例,來實現(xiàn)在一分鐘內(nèi)同一個IP只能訪問10次,感興趣的朋友一起看看吧2023-10-10基于Springboot的高校社團(tuán)管理系統(tǒng)的設(shè)計與實現(xiàn)
本文將基于Springboot+Mybatis開發(fā)實現(xiàn)一個高校社團(tuán)管理系統(tǒng),系統(tǒng)包含三個角色:管理員、團(tuán)長、會員。文中采用的技術(shù)有Springboot、Mybatis、Jquery、AjAX、JSP等,感興趣的可以了解一下2022-07-07詳解springboot使用異步注解@Async獲取執(zhí)行結(jié)果的坑
本文主要介紹了springboot使用異步注解@Async獲取執(zhí)行結(jié)果的坑,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08