1、pom.xml依赖:
<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、config
package com.example.swagger;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* @author liuYC
* @ClassName Config
* @Description TODO
* @date 2021/8/4 9:58
*/
@Configuration
@EnableSwagger2
public class Config implements WebMvcConfigurer {
// 可以又可以没有,达到yml文件配置生效还是不生效,看个人的情况!
/* @Value(value = "${spring.swagger2.enabled}")
private Boolean swaggerEnabled;*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(true)
.select()
// 指定对应的包下的接口
.apis(RequestHandlerSelectors.basePackage("com.example.swagger"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("接口文档")
.description("liuyuchao swagger Spring Boot test")
// http://127.0.0.1:8080/swagger-ui.html
.termsOfServiceUrl("https://***个人网站信息连接推荐***")
.version("1.0")
.build();
}
}
3、另一种config:
package com.macro.mall.search.config;
import com.macro.mall.common.config.BaseSwaggerConfig;
import com.macro.mall.common.domain.SwaggerProperties;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* Swagger API文档相关配置
* Created by macro on 2018/4/26.
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig extends BaseSwaggerConfig {
@Override
public SwaggerProperties swaggerProperties() {
return SwaggerProperties.builder()
.apiBasePackage("com.macro.mall.search.controller")
.title("mall搜索系统")
.description("mall搜索相关接口文档")
.contactName("macro")
.version("1.0")
.enableSecurity(false)
.build();
}
}
4、在controller的中典型注解使用
@Api(description = "用户接口")
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@ApiOperation("数据库初始化100条数据")
@RequestMapping(value = "/init", method = RequestMethod.GET)
public void init() {
for (int i = 0; i < 100; i++) {
Random rand = new Random();
User user = new User();
String temp = "un" + i;
user.setUsername(temp);
user.setPassword(temp);
int n = rand.nextInt(2);
user.setSex((byte) n);
userService.createUser(user);
}
}}
5、在model类的使用方式:
@ApiModel(value = "用户信息")
@ApiModelProperty(value = "创建时间")
@ApiModel(value = "用户信息")
public class UserVO {
@ApiModelProperty(value = "用户ID")
private Integer id;
@ApiModelProperty(value = "用户名")
private String username;
@ApiModelProperty(value = "密码")
private String password;
@ApiModelProperty(value = "性别 0=女 1=男 ")
private Byte sex;
@ApiModelProperty(value = "删除标志,默认0不删除,1删除")
private Byte deleted;
@ApiModelProperty(value = "更新时间")
private Date updateTime;
@ApiModelProperty(value = "创建时间")
private Date createTime;
}
6、访问地址:启动端口加上后缀/swagger-ui.html
http://127.0.0.1:8080/swagger-ui.html
7、测试

点击 tryout 会提示要求输入的参数,可以达到自己的测试要求
8、完整的demo演示:
链接:https://pan.baidu.com/s/1JeT9hk1ZRFKatTplUS-Vdg
提取码:v3my
--来自百度网盘超级会员V5的分享
9、一些个人思考?以及原理参考:
Swagger天天用,但它背后的实现原理很多人都不知道! - 里奥ii的文章 - 知乎 https://zhuanlan.zhihu.com/p/337261486
10、同类API接口管理工具的拓展何使用
docway 、swagger 、postman,rap,yapi。

6478

被折叠的 条评论
为什么被折叠?



