360 推出無追搜尋:體驗純淨,更少廣告
阿新 • • 發佈:2021-12-27
整合Swagger
依賴匯入
<!-- 使用swagger --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version> </dependency> <!-- swagger美化 --> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>swagger-bootstrap-ui</artifactId> <version>1.9.3</version> </dependency>
注意事項:spring版本2.6.0會報錯,建議回滾到2.5.0版本。
配置Swagger
package com.cao.frs.configuration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; 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; import java.util.ArrayList; @Configuration @EnableSwagger2 //開啟swagger public class MySwaggerConfig{ @Bean //建立swagger Bean例項 public Docket docker(){ return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .groupName("財務系統") //組名 .select() .apis(RequestHandlerSelectors.basePackage("com.cao.frs.controller"))//掃描控制器路徑 .paths(PathSelectors.any()) //過濾 .build(); } private ApiInfo apiInfo(){ Contact contact = new Contact("曹佳銘", "https://www.cnblogs.com/benbicao/", "[email protected]"); return new ApiInfo("財務管理系統", "後臺介面", "1.0", "https://www.cnblogs.com/benbicao/", contact, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList()); } }
使用Swagger
實體類註解@ApiModel
實體類屬性@ApiModelProperty
控制器註解@Api
控制器方法註解@ApiOperation