#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; }
#默认访问地址