国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

springboot中不能獲取post請求參數的解決方法

瀏覽:265日期:2024-09-09 08:21:16

問題描述

最近在做微信小程序,用的spring boot做后端,突然發現客戶端發送post請求的時候服務端接收不到參數。問題簡化之后如下:

微信小程序端:

在頁面放一個按鈕進行測試

<!--index.wxml--><view class='container'> <button catchtap=’testpost’>點擊進行測試</button></view>

綁定一個函數發送post請求

//index.js//獲取應用實例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) } }) }})

如圖所示:

springboot中不能獲取post請求參數的解決方法

服務端

服務端新建一個springBoot項目,配置端口和路徑

server.port=8081server.servlet.context-path=/testpost

再新建一個controller用于測試:

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 測試post請求參數傳遞 */@RestControllerpublic 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'; }}

可見,如果能獲取到參數的話就會在控制臺打印參數。但是在小程序界面點擊按鈕之后,服務端并不能獲取到數據,如下:

springboot中不能獲取post請求參數的解決方法

解決方法

查閱資料之后發現,post請求提交數據有四種常見方式:

application/x-www-form-urlencoded瀏覽器的原生 <form> 表單,其中ajax也是用這種方式提交的multipart/form-data表單上傳文件用的這種提交方式application/json這種提交方式的消息主體是一個json字符串text/xml消息主體是XML格式的內容再回到小程序中,檢查消息頭發現這里的提交方式為:application/json

springboot中不能獲取post請求參數的解決方法

所以在服務端進行接收的時候不能直接用參數接受,可以以流的形式來讀取json字符串,在用工具類來解析json數據,如下:

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 測試post請求參數傳遞 */@RestControllerpublic 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'; }}

輸出如下:

springboot中不能獲取post請求參數的解決方法

上面用到的解析json的工具類:

<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.28</version></dependency>

使用@RequestBody注解

@RequestBody是作用在形參列表上,用于將前臺發送過來固定格式的數據【xml 格式或者 json等】封裝為對應的 JavaBean 對象。所以上面代碼可以改為如下形式:

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 lijing2. @date 2019-03-31-20:193. @discroption 測試post請求參數傳遞*/@RestControllerpublic 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.*;@Dataclass Person{private String name;private String age;}

到此這篇關于springboot中不能獲取post請求參數的解決方法的文章就介紹到這了,更多相關springboot不能獲取post內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

相關文章:
主站蜘蛛池模板: 雷山县| 上饶市| 丽江市| 汤原县| 金湖县| 通城县| 沛县| 同仁县| 金山区| 莎车县| 旌德县| 万州区| 隆林| 开江县| 绍兴市| 灵川县| 武平县| 唐海县| 富蕴县| 伊宁市| 那坡县| 天等县| 广州市| 无棣县| 顺昌县| 沙雅县| 冀州市| 江油市| 台南县| 团风县| 平武县| 札达县| 灌云县| 济南市| 韶关市| 昌都县| 石泉县| 云林县| 三江| 麟游县| 双江|