SpringBoot2.0高階案例(05):整合 Swagger2 ,構建介面管理介面
一、Swagger2簡介
1、Swagger2優點
整合到Spring Boot中,構建強大RESTful API文件。省去介面文件管理工作,修改程式碼,自動更新,Swagger2也提供了強大的頁面測試功能來除錯RESTful API。
2、Swagger2常用註解
Api:修飾整個類,描述Controller的作用 ApiOperation:描述一個類的一個方法,或者說一個介面 ApiParam:單個引數描述 ApiModel:用物件來接收引數 ApiProperty:用物件接收引數時,描述物件的一個欄位 ApiResponse:HTTP響應其中1個描述 ApiResponses:HTTP響應整體描述 ApiIgnore:使用該註解忽略這個API ApiError :發生錯誤返回的資訊 ApiImplicitParam:一個請求引數 ApiImplicitParams:多個請求引數
二、與SpringBoot2.0 整合
1、核心依賴
spring-boot:2.1.3.RELEASE
swagger:2.6.1
2、Swagger2 配置
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; /** * Swagger 配置檔案 */ @Configuration public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.swagger.two")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("SpringBoot利用Swagger構建API文件") .description("使用RestFul風格, 建立人:知了一笑") .termsOfServiceUrl("https://github.com/cicadasmile") .version("version 1.0") .build(); } }
3、啟動類添加註解
@EnableSwagger2
@SpringBootApplication
public class SwaggerApplication {
public static void main(String[] args) {
SpringApplication.run(SwaggerApplication.class,args) ;
}
}
4、啟動效果圖
三、增刪改查案例
1、新增使用者
(1)、程式碼塊
@ApiOperation(value="新增使用者", notes="建立新使用者") @ApiImplicitParam(name = "user", value = "使用者詳細實體user", required = true, dataType = "User") @RequestMapping(value = "/addUser", method = RequestMethod.POST) public ResponseEntity<JsonResult> addUser (@RequestBody User user){ JsonResult result = new JsonResult(); try { users.put(user.getId(), user); result.setResult(user.getId()); result.setStatus("ok"); } catch (Exception e) { result.setResult("服務異常"); result.setStatus("500"); e.printStackTrace(); } return ResponseEntity.ok(result); }
(2)、效果圖
2、使用者列表
(1)、程式碼塊
@ApiOperation(value="使用者列表", notes="查詢使用者列表")
@RequestMapping(value = "/getUserList", method = RequestMethod.GET)
public ResponseEntity<JsonResult> getUserList (){
JsonResult result = new JsonResult();
try {
List<User> userList = new ArrayList<>(users.values());
result.setResult(userList);
result.setStatus("200");
} catch (Exception e) {
result.setResult("服務異常");
result.setStatus("500");
e.printStackTrace();
}
return ResponseEntity.ok(result);
}
(2)、效果圖
3、使用者查詢
(1)、程式碼塊
@ApiOperation(value="使用者查詢", notes="根據ID查詢使用者")
@ApiImplicitParam(name = "id", value = "使用者ID", required = true, dataType = "Integer", paramType = "path")
@RequestMapping(value = "/getUserById/{id}", method = RequestMethod.GET)
public ResponseEntity<JsonResult> getUserById (@PathVariable(value = "id") Integer id){
JsonResult result = new JsonResult();
try {
User user = users.get(id);
result.setResult(user);
result.setStatus("200");
} catch (Exception e) {
result.setResult("服務異常");
result.setStatus("500");
e.printStackTrace();
}
return ResponseEntity.ok(result);
}
(2)、效果圖
4、更新使用者
(1)、程式碼塊
@ApiOperation(value="更新使用者", notes="根據Id更新使用者資訊")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "使用者ID", required = true, dataType = "Long",paramType = "path"),
@ApiImplicitParam(name = "user", value = "使用者物件user", required = true, dataType = "User")
})
@RequestMapping(value = "/updateById/{id}", method = RequestMethod.PUT)
public ResponseEntity<JsonResult> updateById (@PathVariable("id") Integer id, @RequestBody User user){
JsonResult result = new JsonResult();
try {
User user1 = users.get(id);
user1.setUsername(user.getUsername());
user1.setAge(user.getAge());
users.put(id, user1);
result.setResult(user1);
result.setStatus("ok");
} catch (Exception e) {
result.setResult("服務異常");
result.setStatus("500");
e.printStackTrace();
}
return ResponseEntity.ok(result);
}
(2)、效果圖
5、刪除使用者
(1)、程式碼塊
@ApiOperation(value="刪除使用者", notes="根據id刪除指定使用者")
@ApiImplicitParam(name = "id", value = "使用者ID", required = true, dataType = "Long", paramType = "path")
@RequestMapping(value = "/deleteById/{id}", method = RequestMethod.DELETE)
public ResponseEntity<JsonResult> deleteById (@PathVariable(value = "id") Integer id){
JsonResult result = new JsonResult();
try {
users.remove(id);
result.setResult(id);
result.setStatus("ok");
} catch (Exception e) {
result.setResult("服務異常");
result.setStatus("500");
e.printStackTrace();
}
return ResponseEntity.ok(result);
}
(2)、效果圖
四、原始碼
GitHub地址:知了一笑
https://github.com/cicadasmile/middle-ware-parent
碼雲地址:知了一笑
https://gitee.com/cicadasmile/middle-ware-parent
相關推薦
SpringBoot2.0高階案例(05):整合 Swagger2 ,構建介面管理介面
一、Swagger2簡介 1、Swagger2優點 整合到Spring Boot中,構建強大RESTful API文件。省去介面文
SpringBoot2.0高階案例(02) :整合 RocketMQ ,實現請求非同步處理
本文原始碼 碼雲地址:知了一笑 https://gitee.com/cicadasmile/middle-ware-parent
SpringBoot2.0高階案例(03):整合 JavaMail ,實現非同步傳送郵件
本文原始碼 碼雲地址:知了一笑 https://gitee.com/cicadasmile/middle-ware-parent
SpringBoot2.0高階案例(06):整合 QuartJob ,實現定時器實時管理
一、QuartJob簡介 1、一句話描述 Quartz是一個完全由java編寫的開源作業排程框架,形式簡易,功能強大。 2、核心A
SpringBoot2.0高階案例(09):整合 ElasticSearch框架,實現高效能搜尋引擎
本文原始碼 碼雲地址:知了一笑 https://gitee.com/cicadasmile/middle-ware-parent
SpringBoot2.0高階案例(12):整合 SpringSecurity 框架,實現使用者許可權安全管理
一、Security簡介 1、基礎概念 Spring Security是一個能夠為基於Spring的企業應用系統提供宣告式的安全訪
SpringBoot2.0高階案例(10):整合 JWT 框架,解決Token跨域驗證問題
GitHub原始碼地址:知了一笑 https://github.com/cicadasmile/middle-ware-paren
SpringBoot2.0高階案例(08):整合 Dubbo框架 ,實現RPC服務遠端呼叫
一、Dubbo框架簡介 1、框架依賴 圖例說明: 1)圖中小方塊 Protocol, Cluster, Proxy, Servi
SpringBoot2.0高階案例(07) 整合:Redis叢集 ,實現訊息佇列場景
本文原始碼 GitHub地址:知了一笑 https://github.com/cicadasmile/middle-ware-pa
SpringBoot2高階案例(11):整合 FastDFS 中介軟體,實現檔案分散式管理
一、FastDFS簡介 1、FastDFS作用 FastDFS是一個開源的輕量級分散式檔案系統,它對檔案進行管理,功能包括:檔案儲
Shiro:整合swagger2時需要放行的資源
filter jar anon spa lte pan 需要 code 整合 filterMap.put("/swagger-ui.html", "anon"); filterMap.put("/swagger-resources/**
SpringBoot--01.SpringBoot2.0入門案例
一、SpringBoot簡介 1、SpringBoot概述(簡化配置、開箱即用) - 為所有 Spring 的開發者提供一個非常快速的、廣泛接受的入門體驗 - 開箱即用(啟動器starter-其實就是SpringBoot提供的一個jar包),但通過自己設定參 (.properties),
第六篇:Spring Boot整合Swagger2構建RESTful API文件
由於Spring Boot有快速開發、便捷部署等特性,所以很大一部分Spring Boot的使用者會用來構建RESTfulAPI。而我們構建RESTfulAPI的目的通常都是由於多終端的原因,這些終端會共用很多底層業務邏輯,因此我們會抽象出這樣一層來同時服務於多個移動端或者Web前端。
微服務 SpringBoot 2.0(九):整合Mybatis
我是SQL小白,我選Mybatis —— Java面試必修 引言 在第五章我們已經整合了Thymeleaf頁面框架,第七章也整合了JdbcTemplate,那今天我們再結合資料庫整合Mybatis框架 在接下來的文章中,我會用一個開源的部落格原始碼來做講解
Springboot2.0啟動報錯:java.lang.NoClassDefFoundError: ch/qos/logback/core/spi/LifeCycle
springboot2.0啟動報錯: java.lang.NoClassDefFoundError: ch/qos/logback/core/spi/LifeCycle at java.lang.ClassLoader.defineClass1(Native Met
springBoot2.0 MyBatis Redis 及RedisCache 整合附demo
springboot2.0 + mybatis 或者 springboot2.0 + redis 在網上可以找到很多資料,但是大都不全或者有這樣那樣的問題,所以便自己動手寫了個demo,能只用 yaml 配置的,儘量不再寫程式碼。 pom.xml <?xml ver
springboot學習入門簡易版四---springboot2.0靜態資源訪問及整合freemarker視圖層
nbsp 規則 pri stat path 整合 位置 啟動程序 -- 2.4.4 SpringBoot靜態資源訪問(9) Springboot默認提供靜態資源目錄位置需放在classpath下,目錄名需要符合如下規則 /static /public /resour
Spring Boot 2.X(十五):整合 Swagger2 開發 API 文件(線上+離線)
前言 相信很多後端開發在專案中都會碰到要寫 api 文件,不管是給前端、移動端等提供更好的對接,還是以後為了以後交接方便,都會要求寫 api 文件。 而手寫 api 文件的話有諸多痛點: 文件更新的時候,需要再次傳送給對接人 介面太對,手寫文件很難管理 介面返回的結果不明確 不能直接線上測試介面,通常需要使
微服務架構案例(05):SpringCloud 基礎元件應用設計
本文原始碼:GitHub·點這裡 || GitEE·點這裡 更新進度(共6節): 01:專案技術選型簡介,架構圖解說明 02:業務架構設計,系統分層管理 03:資料庫選型,業務資料設計規劃 04:中介軟體整合,公共服務管理 05:SpringCloud 基礎元件應用設計 一、元件應用規劃 1、註冊中心
整合swagger2生成Restful Api介面文件 webapi文件描述-swagger
整合swagger2生成Restful Api介面文件 swagger Restful文件生成工具 2017-9-30 官方地址:https://swagger.io/docs/specification/about/ 官方Github:https://github.com/swagger-