5分鐘構建springboot、maven、swagger專案
阿新 • • 發佈:2019-01-02
本文以寫demo的形式講述如何構建一個springboot專案,使用的專案管理及自動構建工具為maven,便於自測及API文件閱讀使用的是swagger。
首先以為新手構建各種踩坑,推薦快速構建網站:start.spring.io,移步於此,可一分鐘構建出springboot、maven專案,選擇自己所對應的語言、版本及專案名稱即可。然後點選”Generate Progect”生成專案。
下載後解壓,匯入編譯器即可。關於配置swagger專案,先向專案中的pom.xm檔案新增如下依賴:
<dependency>
<groupId> org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId >org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
然後與啟動類同一層級出建一個swagger的配置類,類的內容如下:
package com.example.swaggerDemo;
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;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* @author liangxu
* @create 2018年1月15日下午5:29:09
* @version 1.0
*
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
// 指定controller存放的目錄路徑
.apis(RequestHandlerSelectors.basePackage("com.example.swaggerDemo"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("swaggerDemo")
.description("更多詳情訪問我的github:https://github.com/wangnian666/")
.termsOfServiceUrl("https://github.com/wangnian666/")
.contact("liangxu")
.version("1.0")
.build();
}
}
務必指定自己controller所在目錄
然後就可以愉快的在介面處添加註解以便生成對應文件啦,介面示例如下:
@Api("swagger測試")
@RestController
@RequestMapping(value="/")
public class DemoController {
@ApiOperation(value="方法測試",notes="根據傳參返回對應的值")
@RequestMapping(value="/test",method=RequestMethod.GET)
public String word( @RequestParam("word") String word){
return word;
}
}
@Api,是標註一個Controller(Class)為一個swagger資源(API)。
@ApiOperation,對一個操作或HTTP方法進行描述.還有很多其他swagger註解就不一一介紹了,可以自行谷歌查閱相關文件。此篇文件只介紹專案如何搭建swagger,需要具體的資訊可以自行查閱相關注解了解學習。到此告一段落,在瀏覽器中輸入網址:http://localhost:8080/swagger-ui.html,我們看一下demo效果吧。
如果仍然沒有搭建成功可去我的github上下載原始碼,專案地址為swaggerDemo