#pom依賴
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>2.0.2</version> <exclusions> <exclusion> <artifactId>swagger-models</artifactId> <groupId>io.swagger</groupId> </exclusion> </exclusions> </dependency>
#配置類
@Configuration @EnableSwagger2 public class Swagger2Config { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .select() //為當前包下controller生成API文檔 .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller")) .paths(PathSelectors.any()) .build() //添加登錄認證 .apiInfo(apiInfo()) .useDefaultResponseMessages(false); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("api文檔標題") .description("api文檔描述") .contact(new Contact("聯系人", "www.example.com", "郵箱地址")) .version("版本號") .build(); } }
#注解使用
controller:
@Api(tags = "系統管理")
method:
@ApiOperation("用戶管理")
entity:
@ApiModel(value = "用戶信息") public class user{ @ApiModelProperty(value = "用戶信息") private String username; }
#默認訪問地址