作者:w3cbj.cn | 来源:互联网 | 2023-09-17 14:42
前言
swagger2 作为API接口生成的利器,极大的方便了前后端调试的成本。后端研发人员无需关注接口文档的梳理,可以以界面话的方式友好呈现出来。
实战
- pom.xml文件引入jar包:
<dependency><groupId>io.springfoxgroupId><artifactId>springfox-swagger2artifactId><version>2.6.1version>dependency><dependency><groupId>io.springfoxgroupId><artifactId>springfox-swagger-uiartifactId><version>2.6.1version>dependency>
- springboot 的启动主类&#xff0c;添加一下注解&#xff1a;
&#64;EnableSwagger2
- 新建swagger2的配置类&#xff1a;
package com.plugs.swagger;import org.springframework.context.annotation.Bean;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;public class Swagger2Config {&#64;Beanpublic Docket createRestApi() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().paths(PathSelectors.any()).build();}private ApiInfo apiInfo() {return new ApiInfoBuilder().title("springboot利用swagger构建api文档").description("").termsOfServiceUrl("").version("1.0").build();}}
-
访问&#xff1a;ip&#xff1a;端口/项目名/swagger-ui.html
-
获取完整源码地址&#xff1a; https://download.csdn.net/download/penggerhe/11670196
-
公众号关注&#xff0c;免费领取&#xff1a;