Springboot配置swaggerui restFul接口文档

为什么要使用swaggerui 在线文档。

1  如果项目才有前后分离的架构方式。ajax交互是必不可免的。当然这就会后台就会提供接口文档这个swaggerui还有可以在线测试 swaggerui基于boostart文档方式。swaggerui也就是最好的选择

废话不多说,直接上代码

1: pom.xml。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<!-- Spring Boot 版本-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
</parent>
<!--生成api-->
<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> -->
<dependency>
<groupId>com.drore.cloud</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.4</version>
</dependency>

2:配置swaggerui 自动化配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
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;
@Configuration
@EnableSwagger2
@ConditionalOnProperty(prefix = "common",name="enable", matchIfMissing=true)
public class SwaggerConfiguration {
ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("项目API")
.description("提供服务端restful在线接口文档,若需求变更,请刷新页面重新获取信息。")
.termsOfServiceUrl("")
.version("1.0.0")
.contact(new Contact("lwq","","wqli@ewininfo.com"))
.termsOfServiceUrl("http://localhost:8089/")
.build();
}
@Bean
@ConditionalOnMissingBean()//容器中如果没有Docket这个类,那么自动配置这个Docket
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()// 选择那些路径和api会生成document
.apis(RequestHandlerSelectors.basePackage("com.company.project.web"))// 对所有包进行监控
.paths(PathSelectors.any())// 对所有路径进行监控
.build();
}
}

访问http://ip:port/项目名称/doc.html 可以查看