Vue調(diào)用springboot后臺(tái)接口傳參

springboot接口如下:

package com.hkj.springboot.apiForVue.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author liujy
 * @description 測試接口
 * @since 2021-02-19 11:25
 */
@RestController
@RequestMapping("/test")
public class TestParamController {
    /**
     * 測試get請(qǐng)求PathVariable傳參
     */
    @GetMapping("/getPathVariable/{username}/{pwd}")
    public String testGetPathVariable(@PathVariable("username") String username, @PathVariable("pwd") String pwd) {
        String result = username + " " + pwd;
        System.out.println(result);
        return result;
    }

    /**
     * 測試get請(qǐng)求RequestParam傳參
     */
    @GetMapping("/getRequestParam")
    public String testGetRequestParam(@RequestParam("username") String username, @RequestParam("pwd") String pwd) {
        String result = username + " " + pwd;
        System.out.println(result);
        return result;
    }

    /**
     * 測試post請(qǐng)求RequestParam傳參
     */
    @PostMapping("/postRequestParam")
    public String testPostRequestParam(@RequestParam("username") String username, @RequestParam("pwd") String pwd) {
        String result = username + " " + pwd;
        System.out.println(result);
        return result;
    }

    /**
     * 測試post請(qǐng)求RequestBody傳參
     */
    @PostMapping("/postRequestBody")
    public String testPostRequestBody(@RequestBody TestUser testUser) {
        String result = testUser.getUsername() + " " + testUser.getPwd();
        System.out.println(result);
        return result;
    }

    /**
     * 測試post請(qǐng)求RequestParam饶辙、RequestBody混合傳參
     */
    @PostMapping("/postRequestParamRequestBody")
    public String testPostRequestParamRequestBody(@RequestParam("size") Integer size, @RequestBody TestUser testUser) {
        String result = size + " " + testUser.getUsername() + " " + testUser.getPwd();
        System.out.println(result);
        return result;
    }
}

TestUser model如下:

package com.hkj.springboot.apiForVue.controller;

import lombok.Getter;
import lombok.Setter;

/**
 * @author liujy
 * @description 實(shí)體
 * @since 2021-02-19 11:30
 */
@Getter
@Setter
public class TestUser {
    private String username;
    private String pwd;
}

vue測試頁面如下:

<template>
  <div class="base_container">
    <div class="flex_container">
      <button @click="testGetPathVariable">測試get請(qǐng)求PathVariable傳參</button>
      <button @click="testGetRequestParam">測試get請(qǐng)求RequestParam傳參</button>
      <button @click="testPostRequestParam">測試post請(qǐng)求RequestParam傳參</button>
      <button @click="testPostRequestBody">測試post請(qǐng)求RequestBody傳參</button>
      <button @click="postRequestParamRequestBody">測試post請(qǐng)求RequestParam炸卑、RequestBody混合傳參</button>
    </div>
  </div>
</template>

<script>
  export default {
    name: "test",
    data() {
      return {
        userParam: {
          username: 'zhangsan',
          pwd: '123',
          size: 0
        }
      }
    },
    methods: {
      //  get請(qǐng)求 PathVariable傳參
      async testGetPathVariable() {
        const {data} = await this.$axios.get(`/test/getPathVariable/${this.userParam.username}/${this.userParam.pwd}`);
        console.log("get請(qǐng)求 PathVariable傳參");
        console.log(data);
      },
      //  get請(qǐng)求 RequestParam傳參
      async testGetRequestParam() {
        const {data} = await this.$axios.get('/test/getRequestParam', {params: this.userParam});
        console.log("get請(qǐng)求 RequestParam傳參");
        console.log(data);
      },
      //  Post請(qǐng)求 RequestParam傳參
      async testPostRequestParam() {
        const formData = new FormData();
        formData.append('username', this.userParam.username);
        formData.append('pwd', this.userParam.pwd);
        // 方法1
        const {data: d1} = await this.$axios.post('/test/postRequestParam', formData);
        // 方法2
        const {data: d2} = await this.$axios.post(`/test/postRequestParam?username=${this.userParam.username}&pwd=${this.userParam.pwd}`);
        console.log("Post請(qǐng)求 RequestParam傳參");
        console.log(d1);
        console.log(d2);
      },
      //  Post請(qǐng)求 RequestBody傳參
      async testPostRequestBody() {
        const {data} = await this.$axios.post('/test/postRequestBody', this.userParam);
        console.log("Post請(qǐng)求 RequestBody傳參");
        console.log(data);
      },
      //  Post請(qǐng)求 RequestParam颤介、RequestBody混合傳參
      async postRequestParamRequestBody() {
        const {data} = await this.$axios.post(`/test/postRequestParamRequestBody?size=${this.userParam.size}`, this.userParam);
        console.log("Post請(qǐng)求 RequestParam票从、RequestBody混合傳參");
        console.log(data);
      }
    }
  }
</script>

<style scoped>
</style>

測試結(jié)果如下:


測試結(jié)果
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末掖举,一起剝皮案震驚了整個(gè)濱河市些举,隨后出現(xiàn)的幾起案子表悬,更是在濱河造成了極大的恐慌敞曹,老刑警劉巖胜卤,帶你破解...
    沈念sama閱讀 216,544評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件疆导,死亡現(xiàn)場離奇詭異,居然都是意外死亡瑰艘,警方通過查閱死者的電腦和手機(jī)是鬼,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,430評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來紫新,“玉大人均蜜,你說我怎么就攤上這事∶⒙剩” “怎么了囤耳?”我有些...
    開封第一講書人閱讀 162,764評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我充择,道長德玫,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,193評(píng)論 1 292
  • 正文 為了忘掉前任椎麦,我火速辦了婚禮宰僧,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘观挎。我一直安慰自己琴儿,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,216評(píng)論 6 388
  • 文/花漫 我一把揭開白布嘁捷。 她就那樣靜靜地躺著造成,像睡著了一般。 火紅的嫁衣襯著肌膚如雪雄嚣。 梳的紋絲不亂的頭發(fā)上晒屎,一...
    開封第一講書人閱讀 51,182評(píng)論 1 299
  • 那天,我揣著相機(jī)與錄音缓升,去河邊找鬼鼓鲁。 笑死,一個(gè)胖子當(dāng)著我的面吹牛仔沿,可吹牛的內(nèi)容都是我干的坐桩。 我是一名探鬼主播,決...
    沈念sama閱讀 40,063評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼封锉,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼绵跷!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起成福,我...
    開封第一講書人閱讀 38,917評(píng)論 0 274
  • 序言:老撾萬榮一對(duì)情侶失蹤碾局,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后奴艾,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體净当,經(jīng)...
    沈念sama閱讀 45,329評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,543評(píng)論 2 332
  • 正文 我和宋清朗相戀三年蕴潦,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了像啼。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,722評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡潭苞,死狀恐怖忽冻,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情此疹,我是刑警寧澤僧诚,帶...
    沈念sama閱讀 35,425評(píng)論 5 343
  • 正文 年R本政府宣布遮婶,位于F島的核電站,受9級(jí)特大地震影響湖笨,放射性物質(zhì)發(fā)生泄漏旗扑。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,019評(píng)論 3 326
  • 文/蒙蒙 一慈省、第九天 我趴在偏房一處隱蔽的房頂上張望臀防。 院中可真熱鬧,春花似錦边败、人聲如沸清钥。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,671評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至缕坎,卻和暖如春怖侦,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背谜叹。 一陣腳步聲響...
    開封第一講書人閱讀 32,825評(píng)論 1 269
  • 我被黑心中介騙來泰國打工匾寝, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人荷腊。 一個(gè)月前我還...
    沈念sama閱讀 47,729評(píng)論 2 368
  • 正文 我出身青樓艳悔,卻偏偏與公主長得像,于是被迫代替她去往敵國和親女仰。 傳聞我的和親對(duì)象是個(gè)殘疾皇子猜年,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,614評(píng)論 2 353