Swaggerr
Swaggerr#
- 号称世界上最流行的Api框架;
- RestFul Api 文档在线自动生成工具=>Api文档与API定义同步更新
- 直接运行,可以在线测试API接口;
- 支持多种语言:(Java,Php…)
依赖:#
在项目使用Swagger需要 springfox;
- swagger2
- ui
springboot集成 Swagger#
依赖#
<!-- swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
Swagger配置类#
@Configuration
@EnableSwagger2
public class Swaggerconfiger {
}
访问:http://localhost:8080/swagger-ui.html
Swagger的配置#
文档信息#
@Configuration
@EnableSwagger2
public class Swaggerconfiger {
//Swagger的实例就是Docket
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(getApiInfo());
}
//修改Docket的默认配置
public ApiInfo getApiInfo(){
Contact DEFAULT_CONTACT = new Contact("first-blod", "xxx", "xxxx");
ApiInfo apiInfo = new ApiInfo("dayday 的文档",
"swagger-demo Documentation",
"1.0", "urn:tos",
DEFAULT_CONTACT,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList()
);
return apiInfo;
}
}
扫描的包#
//Swagger的实例就是Docket
@Bean
public Docket docket(){
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.apiInfo(getApiInfo())
//修改要扫描的地方
.select().apis(RequestHandlerSelectors.basePackage("com.example.controller"))
.build();
return docket;
}
分组#
Swagger注释#
Api#
@Api 用在类上,说明该类的作用。可以标记一个 Controller 类作为 Swagger 文档资源,使用方式代码如下所示。
@Api(tags={"用户接口"})
@RestController
public class UserController {
}
ApiModel#
@ApiModel 用在类上,表示对类进行说明,用于实体类中的参数接收说明。使用方式代码如下所示。
@ApiModel(value = "名称",description = "用户")
ApiModelProperty#
@ApiModelProperty() 用于字段,表示对 model 属性的说明。使用方式代码如下所示。
@ApiModelProperty(value = "用户名")
ApiParam#
@ApiParam 用于 Controller 中方法的参数说明。
ApiOperation#
@ApiOperation 用在 Controller 里的方法上,说明方法的作用,每一个接口的定义。使用方式代码如下所示。
- value:接口名称
- notes:详细说明
ApiResponse 和 ApiResponses#
@ApiResponse 用于方法上,说明接口响应的一些信息;@ApiResponses 组装了多个 @ApiResponse。使用方式代码如下所示。
@ApiResponses({ @ApiResponse(code = 200, message = "OK", response = UserDto.**class**) })
ApiImplicitParam 和 ApiImplicitParams#
用于方法上,为单独的请求参数进行说明。使用方式代码如下所示。
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "用户ID", dataType = "string", paramType = "query", required = **true**, defaultValue = "1") })
- name:参数名,对应方法中单独的参数名称。
- value:参数中文说明。
- required:是否必填。
- paramType:参数类型,取值为 path、query、body、header、form。
- dataType:参数数据类型。
- defaultValue:默认值。
作者:Esofar
出处:https://www.cnblogs.com/firsthelloworld/p/13545302.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本