swgger前后端分离api生成
在pom.xml里引入如下代码:
<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>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency>
在application文件上配置注释:@EnableSwagger2
并配置swagger2代码如下:
@Configuration @EnableSwagger2 public class SwaggerObj { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.example.demo_a.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("demo_a") .description("my demo doc dec") .termsOfServiceUrl("http://localhost:8080/") .contact("devin") .version("1.0") .build(); } }
在idea下,spring boot项目启动成功,打开链接:http://localhost:8080/swagger-ui.html
运行结果: