spring boot 中Swagger的使用

在開發過程當中方便測試
一、依賴的包:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-spring-web</artifactId>
<version>2.6.1</version>
</dependency>

二、配置類:
@Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket customDocket() { return new Docket(DocumentationType.SWAGGER_2).groupName("situation") .genericModelSubstitutes(DeferredResult.class).useDefaultResponseMessages(false).forCodeGeneration(true) .select().apis(RequestHandlerSelectors.basePackage("com.huawei.situation.controller"))// 選擇哪些路徑和API會生成document
                .paths(PathSelectors.any())// 對全部路徑進行監控
 .build().apiInfo(apiInfo()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Situation API") .description("TCB_Site_Situation Rest API with Swagger") .termsOfServiceUrl("/Situation/") .contact(new Contact("lwx480324", "", "")) .license("TCB_Site_Situation License") .licenseUrl("") .version("1.0") .build(); } }三、在controller類上使用@Api定義controller的基本信息,在相關api接口上使用@ApiOperation、@ApiResponses來設置方法的一些基本信息、響應類型等信息四、localhost:port/swagger-ui.html訪問頁面!
相關文章
相關標籤/搜索