一.創建可運行的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