SpringBoot與Postman實(shí)現(xiàn)REST模擬請(qǐng)求的操作
前言
Postman是一款Http請(qǐng)求模擬工具.它可以模擬各種Http Request,使用起來十分的方便.
使用背景
利用Spring Boot 快速搭建一個(gè)Web應(yīng)用,利用相同的url,不同的請(qǐng)求方式來調(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); } }
其中需要說明的幾個(gè)注解:
GetMapping/PostMapping/PutMapping/DeleteMapping都是組合注解.
學(xué)習(xí)過SpringMVC的同學(xué)都知道用RequestMapping注解來進(jìn)行映射請(qǐng)求.
而以上四個(gè)注解就是基于Http的REST風(fēng)格的請(qǐng)求+RequestMapping的結(jié)合.
分別代表REST風(fēng)格的CRUD操作.
使用Postman
下載方式:chrome商店搜索Postman即可.(有問題可以來私信我)
如下圖所示,Postman界面為我們提供了多種請(qǐng)求方式
舉個(gè)栗子
利用Put請(qǐng)求使用更新操作
首先選擇請(qǐng)求方式為Put,在Body標(biāo)簽下填寫要傳入的參數(shù),需要注意的是Put請(qǐng)求與其他三種請(qǐng)求方式不一樣,要選擇x-www-form-urlencoded方式提交,而不是form-data.
spring-boot postman post請(qǐng)求遇到的坑
今天用postman調(diào)試接口,發(fā)現(xiàn)post請(qǐng)求進(jìn)不去,一直報(bào)錯(cuò)
get請(qǐng)求是可以的,我就納悶了,難道是我寫接口的姿勢(shì)不對(duì)?
后來逐步分析問題,發(fā)現(xiàn)問題出在了請(qǐng)求頭Header的Content-Type上,
application/x-www-form-urlencoded這個(gè)類型,就報(bào)錯(cuò),
必須要改成application/json,
網(wǎng)上查下資料,大概懂了,
后臺(tái)請(qǐng)求用@RequestBody的話,Content-Type就要設(shè)置為application/json,如果用@RequestParam的話,application/x-www-form-urlencoded這個(gè)格式也是可以的,就是前端數(shù)據(jù)以form方式提交
即application/x-www-form-urlencoded的時(shí)候傳參方式如下
application/json的時(shí)候,傳參方式就是正常的json格式
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java 中模擬UDP傳輸?shù)陌l(fā)送端和接收端實(shí)例詳解
這篇文章主要介紹了java 中模擬UDP傳輸?shù)陌l(fā)送端和接收端實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-03-03Idea工具中使用Mapper對(duì)象有紅線的解決方法
mapper對(duì)象在service層有紅線,項(xiàng)目可以正常使用,想知道為什么會(huì)出現(xiàn)這種情,接下來通過本文給大家介紹下Idea工具中使用Mapper對(duì)象有紅線的問題,需要的朋友可以參考下2022-09-09關(guān)于Java整合RocketMQ實(shí)現(xiàn)生產(chǎn)消費(fèi)詳解
這篇文章主要介紹了關(guān)于Java整合RocketMQ實(shí)現(xiàn)生產(chǎn)消費(fèi)詳解,RocketMQ作為一款純java、分布式、隊(duì)列模型的開源消息中間件,支持事務(wù)消息、順序消息、批量消息、定時(shí)消息、消息回溯等,需要的朋友可以參考下2023-05-05Java設(shè)計(jì)模式之橋梁(Bridge)模式
這篇文章主要介紹了Java設(shè)計(jì)模式之橋梁(Bridge)模式,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)Java設(shè)計(jì)模式的小伙伴們有很好的幫助,需要的朋友可以參考下2021-05-05UrlDecoder和UrlEncoder使用詳解_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了UrlDecoder和UrlEncoder使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07idea項(xiàng)目文件夾橫向顯示,縱向顯示的解決方法
這篇文章主要介紹了idea項(xiàng)目文件夾橫向顯示,縱向顯示的解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08