Springboot整合swagger2
第一步:添加依赖
1 2 3 4 5 6 7 8 9 10 | <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version> 2.9 . 2 </version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version> 2.9 . 2 </version> </dependency> |
第二步:添加配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | package com.example.lianxi.demo.config; import io.swagger.annotations.ApiOperation; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.RequestHandler; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableSwagger2 @ConditionalOnProperty (name = "swagger.enable" , havingValue = "false" ) //havingValue是与配置的值对比值,当两个值相同返回true,配置类生效.public class SwaggerConfigure { @Bean public Docket createDocket(){ Docket docket = new Docket(DocumentationType.SWAGGER_2); docket .select() .paths(PathSelectors.any()) .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) .build(); return docket; } } |
第三步:访问
http://localhost:8081/swagger-ui.html
第四步:常用注解
@Api 用在请求的类上,表示对类的说明
@ApiOperation 用在请求的方法上
@ApiImplicitParams 用在请求的方法上,表示一组参数说明
@ApiImplicitParam:用在@ApiImplicitParams注解中,指定一个请求参数的各个方面
@ApiModel 用在请求的类上,表示对类的说明
@ApiResponses用在请求的方法上,表示一组响应
@ApiParam 用在请求方法中,描述参数信息
例子:
@RestController @RequestMapping("/kettle") @Api(tags = "kettle调度任务") public class KettleDispatchController {}
/** * 调度作业 */ @GetMapping("/job") @ApiOperation(value = "调度作业") public BaseResponse job(@RequestParam("jobName") String jobName) { dispatchJobService.runJob(jobName); return BaseResponse.success(null); }
@Data @ApiModel(value="ConsultProject对象",description="项目留言") public class ConsultProjectDto implements Serializable { /** * 公司名称 */ @ApiModelProperty(value="公司名称",example="百度") private String companyName; /** * 联系人 */ @ApiModelProperty(value="联系人",example="张三") private String contactsName; /** * 联系人电话 */ @ApiModelProperty(value="联系人电话",example="18709980921") private String contactsPhone; /** * 咨询标题 */ @ApiModelProperty(value="咨询标题",example="标题1") private String consultTitle; /** * 咨询内容 */ @ApiModelProperty(value="咨询内容",example="内容") private String consultContent; /** * 咨询对象 */ @ApiModelProperty(value="咨询对象",example="某老师") private String consultMan; /** * 价格 */ @ApiModelProperty(value="价格",example="100") private String price; /** * 成本项目id */ @ApiModelProperty(value="成本项目id") private String topicInfoId; // /** // * 创建人 // */ // private String createBy; // // /** // * 创建时间 // */ // @CreatedDate // private LocalDateTime createDate; // // /** // * 更新人 // */ // private String updateBy; // // /** // * 更新时间 // */ // private LocalDateTime updateDate; }
SpringBoot集成Swagger-Bootstrap-UI,页面更清爽!
https://blog.csdn.net/sihai12345/article/details/115743643
分类:
java
标签:
swagger2
, Springboot整合swagger2
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?