springboot中不能獲取post請(qǐng)求參數(shù)的解決方法
問題描述
最近在做微信小程序,用的spring boot做后端,突然發(fā)現(xiàn)客戶端發(fā)送post請(qǐng)求的時(shí)候服務(wù)端接收不到參數(shù)。問題簡(jiǎn)化之后如下:
微信小程序端:
在頁(yè)面放一個(gè)按鈕進(jìn)行測(cè)試
<!--index.wxml--> <view class="container"> <button catchtap='testpost'>點(diǎn)擊進(jìn)行測(cè)試</button> </view>
綁定一個(gè)函數(shù)發(fā)送post請(qǐng)求
//index.js //獲取應(yīng)用實(shí)例 const app = getApp() Page({ testpost:function(){ wx.request({ url: 'http://127.0.0.1:8081/testpost/demo', method:'POST', data:{ name:'lijing', age:'18' }, success:function(res){ console.log(res); }, fail:function(err){ console.log(err) } }) } })
如圖所示:
服務(wù)端
服務(wù)端新建一個(gè)springBoot項(xiàng)目,配置端口和路徑
server.port=8081 server.servlet.context-path=/testpost
再新建一個(gè)controller用于測(cè)試:
package com.demo.demo; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; /** 1. @author lijing 2. @date 2019-03-31-20:19 3. @discroption 測(cè)試post請(qǐng)求參數(shù)傳遞 */ @RestController public class TestController { @RequestMapping(value = "/demo",method = RequestMethod.POST) public String demo(String name,String age){ System.out.println("name = [" + name + "], age = [" + age + "]"); return "server response"; } }
可見,如果能獲取到參數(shù)的話就會(huì)在控制臺(tái)打印參數(shù)。
但是在小程序界面點(diǎn)擊按鈕之后,服務(wù)端并不能獲取到數(shù)據(jù),如下:
解決方法
查閱資料之后發(fā)現(xiàn),post請(qǐng)求提交數(shù)據(jù)有四種常見方式:
application/x-www-form-urlencoded
瀏覽器的原生 <form> 表單,其中ajax也是用這種方式提交的multipart/form-data
表單上傳文件用的這種提交方式application/json
這種提交方式的消息主體是一個(gè)json字符串text/xml
消息主體是XML格式的內(nèi)容
再回到小程序中,檢查消息頭發(fā)現(xiàn)這里的提交方式為:application/json
所以在服務(wù)端進(jìn)行接收的時(shí)候不能直接用參數(shù)接受,可以以流的形式來讀取json字符串,在用工具類來解析json數(shù)據(jù),如下:
package com.demo.demo; import com.alibaba.fastjson.JSONObject; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * @author lijing * @date 2019-03-31-20:19 * @discroption 測(cè)試post請(qǐng)求參數(shù)傳遞 */ @RestController public class TestController { @RequestMapping(value = "/demo",method = RequestMethod.POST) public String demo(HttpServletRequest req){ try { BufferedReader br = new BufferedReader(new InputStreamReader(req.getInputStream())); StringBuffer sb=new StringBuffer(); String s=null; while((s=br.readLine())!=null){ sb.append(s); } JSONObject jsonObject = JSONObject.parseObject(sb.toString()); String name = jsonObject.getString("name"); String age = jsonObject.getString("age"); System.out.println("name:"+name+" age:"+age); } catch (IOException e) { e.printStackTrace(); } return "server response"; } }
輸出如下:
上面用到的解析json的工具類:
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.28</version> </dependency>
使用@RequestBody注解
@RequestBody是作用在形參列表上,用于將前臺(tái)發(fā)送過來固定格式的數(shù)據(jù)【xml 格式或者 json等】封裝為對(duì)應(yīng)的 JavaBean 對(duì)象。所以上面代碼可以改為如下形式:
package com.demo.demo; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; /** 1. @author lijing 2. @date 2019-03-31-20:19 3. @discroption 測(cè)試post請(qǐng)求參數(shù)傳遞 */ @RestController public class TestController { @RequestMapping(value = "/demo",method = RequestMethod.POST) public String demo(@RequestBody Person person){ System.out.println(person); return "server response"; } }
package com.demo.model; import lombok.*; @Data class Person{ private String name; private String age; }
到此這篇關(guān)于springboot中不能獲取post請(qǐng)求參數(shù)的解決方法的文章就介紹到這了,更多相關(guān)springboot不能獲取post內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot整合camunda+mysql的集成流程分析
本文介紹基于mysql數(shù)據(jù)庫(kù),如何實(shí)現(xiàn)camunda與springboot的集成,如何實(shí)現(xiàn)基于springboot運(yùn)行camunda開源流程引擎,本文分步驟圖文相結(jié)合給大家介紹的非常詳細(xì),需要的朋友參考下吧2021-06-06intelij?idea?2023創(chuàng)建java?web項(xiàng)目的完整步驟
這篇文章主要給大家介紹了關(guān)于intelij?idea?2023創(chuàng)建java?web項(xiàng)目的完整步驟,該教學(xué)主要針對(duì)各位剛剛接觸javaweb開發(fā)的小伙伴,各位學(xué)習(xí)java的朋友也難免會(huì)經(jīng)歷這個(gè)階段,需要的朋友可以參考下2023-10-10SpringBoot對(duì)靜態(tài)資源的映射規(guī)則詳解解讀
這篇文章主要介紹了SpringBoot對(duì)靜態(tài)資源的映射規(guī)則詳解解讀,在Spring Boot中,映射規(guī)則是用來定義URL與控制器方法之間的映射關(guān)系的,通過映射規(guī)則,可以將特定的URL請(qǐng)求映射到相應(yīng)的控制器方法上,從而實(shí)現(xiàn)請(qǐng)求的處理和響應(yīng)的返回,需要的朋友可以參考下2023-10-10Java實(shí)現(xiàn)產(chǎn)生隨機(jī)字符串主鍵的UUID工具類
這篇文章主要介紹了Java實(shí)現(xiàn)產(chǎn)生隨機(jī)字符串主鍵的UUID工具類,涉及java隨機(jī)數(shù)與字符串遍歷、轉(zhuǎn)換等相關(guān)操作技巧,需要的朋友可以參考下2017-10-10