1. 程式人生 > 程式設計 >springboot中不能獲取post請求引數的解決方法

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

問題描述

最近在做微信小程式,用的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=8081
server.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請求引數傳遞
 */
@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";
  }
}

可見,如果能獲取到引數的話就會在控制檯列印引數。
但是在小程式介面點選按鈕之後,服務端並不能獲取到資料,如下:

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請求引數傳遞
 */
@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";
  }
}

輸出如下:

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 lijing
2. @date 2019-03-31-20:19
3. @discroption 測試post請求引數傳遞
*/
@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;
}

到此這篇關於springboot中不能獲取post請求引數的解決方法的文章就介紹到這了,更多相關springboot不能獲取post內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!