一 什么是swagger
Swagger的目标是为REST APIs 定义一个标准的,与语言无关的接口,使人和计算机在看不到源码或者看不到文档或者不能通过网络流量检测的情况下能发现和理解各种服务的功能。当服务通过Swagger定义,消费者就能与远程的服务互动通过少量的实现逻辑。类似于低级编程接口,Swagger去掉了调用服务时的很多猜测。
二 配置swagger的步骤
1.首先是依赖
<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>
2.在项目中建一个Swagger类,且此类与启动类平级,并且启动类上加一个注解@EnableSwagger2.(一定注意:.apis(RequestHandlerSelectors.basePackage(“com.rzzz.inform_reor”))这个双引号里面,必须是你自己完成项目的名称).
@Configuration
@EnableSwagger2
public class Swagger {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.rzzz.inform_reor"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
ApiInfo apiInfo = new ApiInfo("多元融合接口",//大标题
"多元融合接口",//小标题
"1.0",//版本
"",
"Rick",//作者
"xx科技",//链接显示文字
""//网站链接
);
return apiInfo;
}
}
3.启动类上加一个注解@EnableSwagger2
@SpringBootApplication
@EnableSwagger2
public class InformReorApplication {
public static void main(String[] args) {
SpringApplication.run(InformReorApplication.class, args);
}
4.项目启动后,在浏览器上输入这个地址: http://localhost:8080/swagger-ui.html#/
5.demo效果如下
6.这个时候,我们后端就可以自己测试接口了.