Swagger的基本使用

Swagger简介和使用

使用Swagger你只需要按照它的规范去定义接口及接口相关的信息,再通过Swagger衍生出来的一系列项目和工具,就可以做到生成各种格式的接口文档,以及在线接口调试页面等等。
使用Swagger你只需要按照它的规范去定义接口及接口相关的信息,再通过Swagger衍生出来的一系列项目和工具,就可以做到生成各种格式的接口文档,以及在线接口调试页面等等.

官网: https://swagger.io

knife4j是为Java MVC框架集成Swagger生成Api文档的增强解决方案。
是为JavaMVC框架集成Swagger生成Api文档的增强解决方案。

<dependencies>
        <!--swagger-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <scope>provided </scope>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <scope>provided </scope>
        </dependency>
 </dependencies>
   

创建Swagger配置类

//配置类
@Configuration
//swagger注解
@EnableSwagger2
public class swaggerConfig {

    @Bean
    public Docket webApiConfig(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("webApi")
                .apiInfo(webApiInfo())
                .select()
                .paths(Predicates.not(PathSelectors.regex("/admin/.*")))
                .paths(Predicates.not(PathSelectors.regex("/error/.*")))
                .build();
    }

    private ApiInfo webApiInfo(){
        return new ApiInfoBuilder()
                .title("网站-课程中心API文档")
                .description("本文档描述了课程中心微服务接口定义")
                .version("1.0")
                .contact(new Contact("java","http://hmy.com","123@qq.com"))
                .build();
    }

}

因为swaggerconfig使用了@Configuration,要使启动类扫描到,要加注解,不然只扫描当前包下的内容

@SpringBootApplication
@ComponentScan(basePackages = {"com.hmy"})
public class EduApplication {
    public static void main(String[] args) {
        SpringApplication.run(EduApplication.class,args);
    }
}

启动程序

http://localhost:8001/swagger-ui.html

posted @ 2022-05-08 12:30  别遇见  阅读(133)  评论(0)    收藏  举报