SpringBoot整合Swagger2生成Api文件
SpringBoot整合Swagger2
一、新增Swagger2 pom依賴檔案
1、此處為根目錄下pom依賴
- <properties>
- <swagger.version>2.4.0</swagger.version>
- </properties>
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>io.springfox</groupId>
- <artifactId>springfox-swagger-ui</artifactId>
- <version>${swagger.version}</version>
- </dependency>
- <dependency>
- <groupId>io.springfox</groupId>
- <artifactId>springfox-swagger2</artifactId>
- <version>${swagger.version}</version>
- </dependency>
- <!-- Swagger結束 -->
- </dependencies>
- </dependencyManagement>
2、當前工程下依賴
- <!-- Swagger開始 -->
- <dependency>
- <groupId>io.springfox</groupId>
- <artifactId>springfox-swagger-ui</artifactId>
- </dependency>
- <dependency>
- <groupId>io.springfox</groupId>
- <artifactId>springfox-swagger2</artifactId>
- </dependency>
- <!-- Swagger結束 -->
二、新建Swagger2配置類
下面為示例:
- package com.fanxf;
- 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.service.Contact;
- import springfox.documentation.spi.DocumentationType;
- import springfox.documentation.spring.web.plugins.Docket;
- import springfox.documentation.swagger2.annotations.EnableSwagger2;
- /**
- * @author fanxf
- * @date 2018/2/28 10:10
- */
- @EnableSwagger2
- @Configuration
- publicclass Swagger {
- @Bean
- public Docket createRestApi() {
- returnnew Docket(DocumentationType.SWAGGER_2)
- .apiInfo(apiInfo())
- .select()
- .apis(RequestHandlerSelectors.basePackage("com.fanxf.service.demo.controller")) //當前包路徑
- .paths(PathSelectors.any())
- .build();
- }
- //構建 api文件的詳細資訊函式
- private ApiInfo apiInfo() {
- returnnew ApiInfoBuilder()
- .title("api文件") //頁面標題
- .contact(new Contact("fanxf","www.fanxf.com","[email protected]")) //建立人
- .version("1.0") //版本號
- .description("api文件") //描述
- .build();
- }
- }
講解:1、@Configuration註解,讓Spring來載入該類配置,
2、@EnableSwagger2註解來啟用Swagger2。
3、通過buildDocket函式建立Docket的Bean之後,apiInfo()用來建立該Api的基本資訊(這些基本資訊會展現在文件頁面中)。
4、select()函式返回一個ApiSelectorBuilder例項用來控制哪些介面暴露給Swagger來展現,本例採用指定掃描的包路徑
來定義,Swagger會掃描該包下所有Controller定義的API,併產生文件內容(除了被@ApiIgnore註解的API)。
三、controller編寫
程式碼如下:
- package com.fanxf.service.demo.controller;
- import com.fanxf.ColorRespDto;
- import com.fanxf.domain.Style;
- import com.fanxf.service.StyleService;
- import io.swagger.annotations.ApiImplicitParam;
- import io.swagger.annotations.ApiOperation;
- import org.apache.commons.lang.StringUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * @author fanxf
- * @date 2018/2/6 16:13
- */
- @RestController
- @RequestMapping("/demo")
- publicclass ColorController {
- @Autowired
- private StyleService styleService;
- @ApiOperation(value = "查詢車輛顏色", notes = "根據車型id查詢所有顏色")
- @ApiImplicitParam(name = "id", value = "車型id", dataType = "String", paramType = "path")
- @GetMapping("/color/{id}")
- public ColorRespDto getList(@PathVariable String id) {
- Style style = styleService.selectById(id);
- if (null == style) {
- returnnull;
- }
- List<String> list = new ArrayList<String>();
- String[] str = style.getColor().split(".");
- for (String color : str) {
- list.add(color);
- }
- ColorRespDto dto = new ColorRespDto();
- dto.setColor(list);
- return dto;
- }
- }
講解:1、@ApiOperation:用在方法上,說明方法的作用,標註在具體請求上,value和notes的作用差不多,都是對請求進行
說明
2、@ApiImplicitParams:用在方法上包含一組引數說明
3、@ApiImplicitParam:用在@ApiImplicitParams註解中,指定一個請求引數的各個方面
paramType:引數放在哪個地方
header 請求引數的獲取:@RequestHeader
query 請求引數的獲取:@RequestParam
path(用於restful介面) 請求引數的獲取:@PathVariable
body(不常用)
form(不常用)
name:引數名
dataType:引數型別
required:引數是否必須傳
value:引數的意思
defaultValue:引數的預設值
完成controller後啟動springboot
如圖:
可以看到其中的controller
點選然後進行訪問: