溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

SpringBoot與Postman怎么實(shí)現(xiàn)REST模擬請求

發(fā)布時間:2021-06-21 15:46:48 來源:億速云 閱讀:287 作者:chen 欄目:開發(fā)技術(shù)

本篇內(nèi)容介紹了“SpringBoot與Postman怎么實(shí)現(xiàn)REST模擬請求”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

前言

Postman是一款Http請求模擬工具.它可以模擬各種Http Request,使用起來十分的方便.

使用背景

利用Spring Boot 快速搭建一個Web應(yīng)用,利用相同的url,不同的請求方式來調(diào)用不同的方法.最后利用Postman工具模擬實(shí)現(xiàn).

實(shí)現(xiàn)方法

利用IDEA快速構(gòu)建應(yīng)用環(huán)境

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

配置SpringBoot文件application.yml

server:
  port: 8080
  servlet:
    context-path: /girl
spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/test
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: 1234
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

Controller代碼

@RestController
public class MyController {
    @Autowired
    UserDao userDao;
    @RequestMapping(value = "/say/{name}")
    public @ResponseBody User say(@PathVariable("name") String uname){
        User user = new User();
        user.setUname(uname);
        return  userDao.save(user);
    }
    @GetMapping("/a")
    public List<User> geyUserList(){
        return userDao.findAll();
    }
    @PostMapping("/a")
    public User addUser(@RequestParam("uname") String uname){
       User user = new User();
       user.setUname(uname);
       return userDao.save(user);
    }
    @PutMapping(value = "/a/{no}")
    public User updateUser(@PathVariable("no") Integer uno,@RequestParam("uname") String uname){
       User user = new User();
       user.setUno(uno);
       user.setUname(uname);
       return userDao.save(user);
    }
    @DeleteMapping(value = "/a/{no}")
    public void deleteUser(@PathVariable("no") Integer uno){
        userDao.deleteById(uno);
    }
}

其中需要說明的幾個注解:

GetMapping/PostMapping/PutMapping/DeleteMapping都是組合注解.

學(xué)習(xí)過SpringMVC的同學(xué)都知道用RequestMapping注解來進(jìn)行映射請求.

而以上四個注解就是基于Http的REST風(fēng)格的請求+RequestMapping的結(jié)合.

分別代表REST風(fēng)格的CRUD操作.

使用Postman

下載方式:chrome商店搜索Postman即可.(有問題可以來私信我)

如下圖所示,Postman界面為我們提供了多種請求方式

SpringBoot與Postman怎么實(shí)現(xiàn)REST模擬請求

舉個栗子

利用Put請求使用更新操作

SpringBoot與Postman怎么實(shí)現(xiàn)REST模擬請求

首先選擇請求方式為Put,在Body標(biāo)簽下填寫要傳入的參數(shù),需要注意的是Put請求與其他三種請求方式不一樣,要選擇x-www-form-urlencoded方式提交,而不是form-data.

spring-boot postman post請求遇到的坑

今天用postman調(diào)試接口,發(fā)現(xiàn)post請求進(jìn)不去,一直報錯

SpringBoot與Postman怎么實(shí)現(xiàn)REST模擬請求

get請求是可以的,我就納悶了,難道是我寫接口的姿勢不對?

后來逐步分析問題,發(fā)現(xiàn)問題出在了請求頭Header的Content-Type上,

application/x-www-form-urlencoded這個類型,就報錯,

必須要改成application/json,

網(wǎng)上查下資料,大概懂了,

后臺請求用@RequestBody的話,Content-Type就要設(shè)置為application/json,如果用@RequestParam的話,application/x-www-form-urlencoded這個格式也是可以的,就是前端數(shù)據(jù)以form方式提交

即application/x-www-form-urlencoded的時候傳參方式如下

SpringBoot與Postman怎么實(shí)現(xiàn)REST模擬請求

SpringBoot與Postman怎么實(shí)現(xiàn)REST模擬請求

application/json的時候,傳參方式就是正常的json格式

SpringBoot與Postman怎么實(shí)現(xiàn)REST模擬請求

SpringBoot與Postman怎么實(shí)現(xiàn)REST模擬請求

“SpringBoot與Postman怎么實(shí)現(xiàn)REST模擬請求”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI