我們在實際開發過程中經常會遇到一個問題,就是接口文檔。有時候接口還沒開發完但是接口文檔需要先給出,因為其他方也可以根據接口文檔先搞起來。但是有時候在開發過程中會發現當初定義的不是那么完善,就會更改。這個過程又涉及到一些溝通之類的比較繁瑣的事情。有問題出現,就會有有心人出來幫忙解決。一個產品Swagger2應運而生。他可以在我們定義Controller時通過Swagger2注解的方式自動生成文檔,只要服務起著,這個文檔就能一直看到。當接口發生調整時,接口文檔也能跟著調整,是一款非常好用的工具。
廢話不多說,我們繼續看pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
</dependency>
springfox-swagger2是swagger2的核心庫,包括了一些注解及處理邏輯等。springfox-swagger-ui 是頁面的展示。下面我們看看配置文件
package com.shuqi;
import io.swagger.annotations.Api;
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;
@Configuration
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket apiDocket() {
ApiInfo apiInfo = new ApiInfoBuilder().title("title").description("description")
.termsOfServiceUrl("").version("1.0")
.contact(new Contact("name", "", "email")).build();
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo).select()
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class)).paths(PathSelectors.any())
.build();
}
}
@EnableSwagger2表示激活此功能,實現是通過導入進來了Swagger2DocumentationConfiguration這個配置類@Import({Swagger2DocumentationConfiguration.class})
既然是個配置文件,Bean就少不了。ApiInfo是關于Api文件的基礎描述,比如說標題,描述,版本號,聯系人等基礎信息。其中我想說的是
apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
他可以定制要展示的Controller中的文檔,比如這里我定義的是類上加了Api這個注解,那么那些沒有加這個注解的Controller就不會展示到頁面上,所以留給我們的操作空間還是蠻大的。看一眼Controller是怎么定義的
package com.shuqi.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Api
public class HelloController {
@RequestMapping(value = "/hello",method = RequestMethod.GET)
@ApiOperation(value = "hello", notes = "hello")
public String hello(@ApiParam("姓名") @RequestParam String name,@ApiParam("年齡") @RequestParam Integer age) {
return "我的名字叫" + name + ",年齡是" + age;
}
}
可以看到我們再類上加了Api的注解。在方法上加了ApiOperation的注解,標示是一個Api 接口,ApiParam標示對接口參數的描述。
下面我們啟動項目,看一下效果。啟動后,輸入http://localhost:8080/swagger-ui.html,看到下面的頁面
他不僅可以作為接口文檔,還可以測試功能
輸入值后點擊Try it out
使用起來相當方便,搞起來吧。
下節將的內容是:SpringBoot基礎教程(五)——與本地緩存的結合