SpringBoot+SwaggerUI
一.建立可執行的Springboot程式碼
1-1.Maven的依賴庫
<dependencies> <!-- 核心模組,包括自動配置支援、日誌和YAML --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- 測試模組,包括JUnit、Hamcrest、Mockito --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </dependency> <!-- 引入Web模組 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.4.RELEASE</version> </dependency> </dependencies>
1-2. 註冊啟動類
@SpringBootApplication
@EnableSwagger2 //在啟動Swagger的時候需要新增此註解
public class App {
public static void main( String[] args ){
SpringApplication.run(App.class,args);
}
}
1-3.編寫測試類
@RestController public class HelloController { protected static org.slf4j.Logger logger= LoggerFactory.getLogger(HelloController.class); @RequestMapping("/say") public String helloworld(){ logger.debug("訪問hello"); return "Hello world!"; } @RequestMapping("/hello/{name}") public String helloName(@PathVariable String name){ logger.debug("訪問helloName,Name={}",name); return "Hello "+name; } }
1-4.在resouce目錄下建立一個application.properties檔案,裡邊註冊伺服器埠
server.port=8080 //註冊伺服器訪問埠
server.servlet-path=/v1 //註冊swaggerUI的啟動路徑
1-5.執行App類 啟動Springboot 在瀏覽器端訪問的地址如下
http://localhost:8080/say //訪問HelloController類下的helloworld方法
http://localhost:8080/hello/say //訪問HelloController下的helloName方法
--------------------------------------------------分割線--------------------------------------------------
在上邊的springBoot完全可以訪問的情況下 開始進一步組裝SwaggerUI,如果上邊的沒有成功,下邊的就不用試了,成功機率不大,所以在學習的時候,我們需要一步一個臺階,循序漸進,慢慢加重砝碼。
開始在上邊的基礎上組裝SwaggerUI
2-0.依賴包配置
<!-- Swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<!-- END Swagger -->
2-1.建立一個SwaggerConfig
@Configuration
public class SwaggerConfig {
@Value("${server.servlet-path}")
private String pathMapping;
@Bean
public Docket restfulApi() {
System.out.println("http://localhost:8080" + pathMapping + "/swagger-ui.html");
return new Docket(DocumentationType.SWAGGER_2).groupName("RestfulApi")
.genericModelSubstitutes(ResponseEntity.class).useDefaultResponseMessages(true).forCodeGeneration(false)
.pathMapping(pathMapping) // base,最終呼叫介面後會和paths拼接在一起
.select().paths(doFilteringRules()).build().apiInfo(apiInfo());
}
private String initContextInfo() {
StringBuffer sb = new StringBuffer();
sb.append("REST API 設計在細節上有很多自己獨特的需要注意的技巧").append("<br/>")
.append("以下是本專案的API文件");
return sb.toString();
}
/*
* "標題 title",
* "描述 description",
* "termsOfServiceUrl",
* "聯絡郵箱 contact email",
* "許可證的型別 license type",
* "許可證的連結 license url"
*/
private ApiInfo apiInfo(){
ApiInfo apiInfo = new ApiInfo("資料儲存專案 Platform API", // 大標題
initContextInfo(), // 簡單的描述
"1.0.0", // 版本
"服務條款", "後臺開發團隊", // 作者
"The Apache License, Version 2.0", // 連結顯示文字
"https://www.csdn.net"// 網站連結
);
return apiInfo;
}
private Predicate<String> doFilteringRules() {
return or(regex("/rest.*"), regex("/holle.*"));
}
}
2-2.在寫測試類1.
@Api(value = "API - ASFM")
@RestController
@RequestMapping("/rest/Management")
public class ManagementController {
private static final Logger LOG = LoggerFactory.getLogger(ManagementController.class);
@ApiOperation(value = "查詢所有", httpMethod = "POST")
@RequestMapping(value = "/findAll", method = RequestMethod.POST)
public String findAll() {
LOG.info("------>>>>>>>列印");
List<String> l = new ArrayList<>();
l.add("list1");
l.add("list2");
l.add("list3");
return "list"+l;
}
}
2.
@Api(value = "API - TEST",description = "測試swaggerUI")
@RestController
@RequestMapping("/rest/test")
public class TestController {
protected static Logger logger = LoggerFactory.getLogger(TestController.class);
/*
* @ApiOperation(value = "介面說明", httpMethod ="介面請求方式", response ="介面返回引數型別", notes ="介面釋出說明"
*
* @ApiParam(required = "是否必須引數", name ="引數名稱", value ="引數具體描述"
*/
@ApiOperation(value = "測試方法1",httpMethod = "GET",notes = "此處是第一個測試方法")
@RequestMapping(value = "/test", method = RequestMethod.GET)
public void test(String Body){
try {
Body.toString();
logger.debug("---->>>>>>>"+Body.toUpperCase() );
} catch (Exception e) {
e.printStackTrace();
logger.info("執行錯誤"+e.getMessage());
}
}
}
2-3.檢查註解是否完善,特別注意啟動類的@EnableSwagger一定要寫,在application.properties裡註冊Swagger路徑2-4.在瀏覽器位址列輸入如下地址
http://localhost:8080/v1/swagger-ui.html#/
2-5.效果展示圖
至此Deam就完成了,希望可以幫到正在學習的你。也歡迎大家再此討論交流,如有不足希望,希望留言,我儘快糾正。
也可以直接下載程式碼演示 點選開啟連結獲取原始碼 https://gitee.com/zk_lmj/SpringBoot.git
相關推薦
SpringBoot+SwaggerUI
一.建立可執行的Springboot程式碼 1-1.Maven的依賴庫 <dependencies> <!-- 核心模組,包括自動配置支援、日誌和YAML --> <dependency> <grou
SpringBoot整合SwaggerUI自動生成介面文件
SpringBoot整合SwaggerUI自動生成介面文件 一、在pom.xml檔案裡新增SpringBoot的引用配置,程式碼如下: <dependency> <groupId>io.springfox</gro
SwaggerUI+SpringBoot
網上能搜尋到的教程版本大多數要麼太老了,要麼就是行不通,所以在這裡整理一個新版的可用的精簡的說明。 step1:pom.xml 增加依賴 <!-- https://mvnrepository.com/artifact/io.springfox/springfox
springboot系列十二、springboot整合RestTemplate、swaggerUI
一、背景介紹 在微服務都是以HTTP介面的形式暴露自身服務的,因此在呼叫遠端服務時就必須使用HTTP客戶端。我們可以使用JDK原生的URLConnection、Apache的Http Client、Netty的非同步HTTP Client, Spring的RestTemplate。這裡介紹的是RestTe
springboot系列十三、springboot整合swaggerUI
一、Swagger介紹 Swagger能成為最受歡迎的REST APIs文件生成工具之一,有以下幾個原因: Swagger 可以生成一個具有互動性的API控制檯,開發者可以用來快速學習和嘗試API。 Swagger 可以生成客戶端SDK程式碼用於各種不同的平臺上的實現。 Swagger 檔案
SpringBoot整合Swaggerui及問題解決
目前在做前後臺分離專案的後臺介面部分,故在springboot中引入了swaggerui來做restful介面測試。本文首先描述springboot整合swaggerui的過程,其次,講述本人在整合過程中遇到的問題及解決方案。一、springboot整合swaggerui(1
SwaggerUI自動生成API文件(SwaggerUI+SpringBoot)
SwaggerUI+SpringBoot 測試訪問地址 http://localhost:8080/v1/swagger-ui.html#/ 下面是具體配置 Mav
SpringBoot 開發提速神器 Lombok+MybatisPlus+SwaggerUI
導讀 Lombok:可以讓你的POJO程式碼特別簡潔,不止簡單在BO/VO/DTO/DO等大量使用,還有設計模式,物件對比等 MybatisPlus:增加版Mybatis,基礎的資料庫CRUD、分頁等可以直接生成使用,避免了大量的重複低效程式碼,還有資料庫自動Java類,sql檔案等等,比傳統的更賤簡介易用
springboot配置容器
repl ron clas 目的 知識 strong class java 自己 servlet容器配置 Spring Boot快速的原因除了自動配置外,另一個就是將web常用的容器也集成進來並做自動配置,讓使用它的人能更快速的搭建web項目,快速的實現自己的業務目的。什
springboot+rabbitmq整合示例程
param resource pom del actor .cn pri 完全 pan 關於什麽是rabbitmq,請看另一篇文: http://www.cnblogs.com/boshen-hzb/p/6840064.html 一、新建maven工程:springboot
Springboot整合redisCluster集群
.cn 數據 blog 3-9 pom.xml 整合 img 技術 相關 1.pom.xml增加redis支持 2.配置增加redis相關 3.RedisProperties.class讀取配置中的值 4.JedisClusterConfig.class 5.RedisCl
springboot-3-aop
bsp log int onf enc span work artifact param aop存在的目的是進一步解耦, spring支持aspectJ的註解式切面編程 1), [email protected]/* */, [email protec
Springboot(一):入門篇
熱啟動 exp 頁面 tor posit ole 入口 service 主程序 什麽是spring boot spring Boot是由Pivotal團隊提供的全新框架,其設計目的是用來簡化新Spring應用的初始搭建以及開發過程。該框架使用了特定的方式來進行配置,從而使開
springboot(二):web綜合開發
long auto string 資源 implement getter ase 自動添加 自己 上篇文章介紹了spring boot初級教程:spring boot(一):入門篇,方便大家快速入門、了解實踐Spring boot特性;本篇文章接著上篇內容繼續為大家介紹sp
分布式架構springmvc+springboot+springcloud+redis
fast src eight 所有應用 nbsp bootstrap https png 場景 分布式 分布式服務:Dubbo+Zookeeper+Proxy+Restful 分布式消息中間件:KafKa+Flume+Zookeeper 分布式緩存:Redis 分布式
springboot-thymeleaf
read free 格式化 bst orm equals 樣式 sco 3.0 在上篇文章springboot(二):web綜合開發中簡單介紹了一下thymeleaf,這篇文章將更加全面詳細的介紹thymeleaf的使用。thymeleaf 是新一代的模板引擎,在sprin
SwaggerUI ASP.Net WebAPI2
配置文件 文件夾 return 程序 控制臺 目前在用ASP.NET的 WebAPI2來做後臺接口開發,在與前臺做測試的時候,總是需要發送一個demo給他,但是這樣很麻煩的,他還有可能記不住。然後就想到SwaggerUI 生成測試文檔。話不多說,來看看它的具體用法吧!第一步:在VS2015
SpringBoot學習遇到的問題(1) - 配置文件有日誌的debug模式等配置項,為什麽不起作用
boot lease bug fig spa stack 不起作用 網站 cat 這個問題困擾我近乎兩天,通過查找N多資料後終於解決,寫下來共享給大家。 logging.level.root=DEBUG ... 一系列的日誌配置項,都不起作用的原因是springboot啟動
基於SpringBoot + Mybatis實現 MVC 項目
myba ces find json格式 see framework pro select .info 1.預覽: (1)完整項目結構 (2) 創建數據庫、數據表: 【user.sql】 SET FOREIGN_KEY_CHECKS=0;
springBoot+RabbitMQ例子
boot code cer .... ebo set print return cor demo目錄 貼代碼 1.ProducerConfig.java package com.test.config; import org.springframework.amqp.