您好,登錄后才能下訂單哦!
這篇文章運用了實例代碼展示SpringBoot整合swagger實現測試Restful風格api的步驟,代碼非常詳細,可供感興趣的小伙伴們參考借鑒,希望對大家有所幫助。
restful API 命名禁止出現動詞,即用請求方式區分動作。如下:
get 查詢接口
post 新增接口
put 更新接口
delete 刪除接口
@RestController
@RequestMapping("/user")
public class UserController {
@Resource
private UserMapper userMapper;
//@RequestMapping("/listByUser")
@GetMapping("/")
public List<User> listByUser() {
return userMapper.list();
}
//@RequestMapping("/getOneUser")
@GetMapping("/{id}")
public User getOneUser(@PathVariable("id") int id) {
return userMapper.selectByPrimaryKey(id);
}
//@RequestMapping("/addUser")
@PostMapping("/")
public int addUser(User user) {
return userMapper.insert(user);
}
//@RequestMapping("/deleteUser")
@DeleteMapping("/{id}")
public int deleteUser(@PathVariable("id") int id) {
return userMapper.deleteByPrimaryKey(id);
}
@PutMapping("/")
public int updateUser(User user) {
return userMapper.updateByPrimaryKey(user);
}
}
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
.apis(RequestHandlerSelectors.basePackage("xyz.java1024.controller")) 對應controller所處的包
### 3.瀏覽器進入api頁面 地址為:http://localhost:8090/swagger-ui.html
![swagger-ui](https://img-blog.csdnimg.cn/20191218181626864.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTI1NDI1Mg==,size_16,color_FFFFFF,t_70)
### 4.調用get請求接口
![swagger-ui](https://img-blog.csdnimg.cn/20191218181652737.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTI1NDI1Mg==,size_16,color_FFFFFF,t_70)
### 5.對于實體類型的請求參數體,我們在Controller接口加上@RequestBody 可讀性會更加好!
> 修改前
![swagger-ui](https://img-blog.csdnimg.cn/20191218181712559.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTI1NDI1Mg==,size_16,color_FFFFFF,t_70)
> 修改后
![swagger-ui](https://img-blog.csdnimg.cn/20191218181731871.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTI1NDI1Mg==,size_16,color_FFFFFF,t_70)
//@RequestMapping("/addUser")
@PostMapping("/")
public int addUser(@RequestBody User user) {
return userMapper.insert(user);
}
> 重新啟動刷新swagger-ui頁面
![swagger-ui](https://img-blog.csdnimg.cn/201912181817490.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTI1NDI1Mg==,size_16,color_FFFFFF,t_70)
### 5.測試post請求
> 把創建時間和更新時間參數去掉 ,填寫用戶名等參數,成功插入一條數據。
![swagger-ui](https://img-blog.csdnimg.cn/20191218181804811.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTI1NDI1Mg==,size_16,color_FFFFFF,t_70)
### 6.swagger 增強
> 對于實體的某些屬性我們時不需要傳的,并且我們不想它顯示在swagger的請求體中,如 新增用戶的 createdAt和updatedAt 等屬性,我們可以通過在屬性上面添加@ApiModelProperty(hidden = true)注解來隱藏屬性
public class User implements Serializable {
private int id;
private String username; private String tel; private String password; private int status; @ApiModelProperty(hidden = true) private Timestamp createdAt; @ApiModelProperty(hidden = true) private Timestamp updatedAt;
> 此時重啟刷新swagger頁面,再次進入UserController的post可看到少了參數,如圖參數變得相當簡潔!
![swagger-ui](https://img-blog.csdnimg.cn/20191218181914671.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTI1NDI1Mg==,size_16,color_FFFFFF,t_70)
以上就是SpringBoot整合swagger實現測試Restful風格api的方法介紹,詳細使用情況還得要大家自己使用過才能知道具體要領。如果想閱讀更多相關內容的文章,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。