swagger2 接口文档
1,maven:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.aiyuesheng</groupId> <artifactId>springcloud-swagger2</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> </parent> <!-- 管理依赖 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.M7</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- SpringBoot整合Web组件 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
<!-- 如果要注册到eureka上,就可以把这个jar 包放开,否则会报错,放开之后,要先开启eureka--> <!-- SpringBoot整合eureka客户端 --> <!-- <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> --> <!-- swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.8.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.8.0</version> </dependency> </dependencies> <!-- 注意: 这里必须要添加, 否者各种依赖有问题 --> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/libs-milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
配置:
package com.aiyuesheng.config; @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select() // api扫包 .apis(RequestHandlerSelectors.basePackage("com.aiyuesheng.api")).paths(PathSelectors.any()).build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder().title("微服务电商系统").description("微服务电商系统接口文档") .termsOfServiceUrl("http://www.aiyuesheng.com") // .contact(contact) .version("1.0").build(); } }
service 接口:
@Api("Swagger Sample") @RestController public class SwaggerService { @ApiOperation("swagger 注解") @PostMapping("/swaggerIndex") public String swaggerIndex() { System.out.println("swaggerIndex"); return "swaggerIndex"; } }
启动类:
@SpringBootApplication public class SwaggerApp { public static void main(String[] args) { SpringApplication.run(SwaggerApp.class, args); } }
端口可以本地配置:
http://localhost:8080/swagger-ui.html#/swagger-controller
Aimer,c'est partager