Swagger
Swagger,可以动态生成接口文档,方便测试与对外沟通。
使用
1.添加依赖
<!-- swagger2核心包 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- swagger-ui 可视化界面 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
2.添加SwaggerConfig配置文件
package com.chimy.springboot.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;
import java.util.ArrayList;
@Configuration
@EnableSwagger2 // 开启Swagger的使用
public class SwaggerConfig {
@Bean // Swagger的使用主要是要将docket对象传入IOC容器
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo()) //关于文档的各种信息
.enable(true) //使Swagger生效
.select()//选择扫描的接口
.apis(RequestHandlerSelectors.basePackage("com.chimy.springboot.controller"))//指定扫描的接口
.build();
}
public ApiInfo apiInfo(){
//个人的联系方式
Contact contact = new Contact("Chimy","https://blog.csdn.net/chimycmy?spm=1001.2101.3001.5343","demo@qq.com");
//文档的各种信息
return new ApiInfo("demo开发文档", "chimy的开发文档", "1.0", "urn:tos",null, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList());
}
}
3.在接口添加注解描述等信息
package com.chimy.springboot.controller;
import com.chimy.springboot.bean.Author;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@Api("hello接口")
@RestController
public class HelloController {
@Resource
private Author author;
@ApiOperation("测试接口")
@GetMapping("/hello")
public String hello(){
return "hello world " + author.getName() + author.getUrl();
}
}
效果
登录地址+/swagger-ui.html
http://localhost:8989/swagger-ui.html
参考:https://baijiahao.baidu.com/s?id=1689028738781297410&wfr=spider&for=pc
同事推荐的接口管理平台:yapi