springboot配置swagger2线上文档
1、先上项目配置好的swagger2的ui界面:
2、需要swagger2的这两个包:
<!-- swagger2 包 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>${springfox-swagger2-version}</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>${springfox-swagger2-version}</version> </dependency>
3、然后需要写配置swagger2的代码类:
代码内容如下:
package com.example.cloudorderdemo.config; import io.swagger.annotations.Api; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; 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; @Configuration public class SwaggerConfig {
//application.yml中配置的version @Value("${info.app.version}") private String version;
//application.yml中配置的应用名称 @Value("${spring.application.name}") private String applicationName; @Bean public Docket createRestApi(){//如果在application.yml中配置了项目访问路径userdemo就要在swagger访问路径的端口后面加上 /userdemo/ //最新:swagger的访问路径:http://localhost:8880/swagger-ui.html# return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).enable(true) .select() .apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) .paths(PathSelectors.any()) .build() .apiInfo(apiInfo()); } private ApiInfo apiInfo(){ return new ApiInfoBuilder() .title("order订单服务") .version(version) .build(); } }
4、最后在application启动类上面加上注解:@EnableSwagger2
5、controller类上面写上接口swagger的注释:
类名上:
方法名上:
最后启动项目打开swagger文档地址:http://localhost:8882/swagger-ui.html#/
。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本