快速搭建spring boot 的 Swagger

简介: 快速搭建spring boot 的 Swagger

快速搭建一个Swagger

1、新建一个maven

2、导入依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.6.0</version>
        <relativePath/>
    </parent>
    <groupId>org.swaggerTest</groupId>
    <artifactId>swaggerTest</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

3、新建Application

package com.rw;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
 * @author yl
 * @createDate 2022/5/25
 * @description
 */
@SpringBootApplication
public class rwApplication {
    public static void main(String[] args) {
        SpringApplication.run(rwApplication.class,args);
    }
}

4、新建一个application.yml文件

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

5、新建一个配置文件SwaggerConfig

package com.rw.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
/**
 * @author yl
 * @createDate 2022/5/25
 * @description
 *
 * 访问地址 :http://127.0.0.1:8080/swagger-ui/index.html
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.rw"))
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo apiInfo(){
        Contact DEFAULT_CONTACT = new Contact("yl", "www.baidu.com", "12345@qq.com");
        return new ApiInfo(
                "StudySwagger",
                "YL",
                "1.0",
                "urn:tos",
                DEFAULT_CONTACT,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList<>());
    }
}

6、运行项目

 

相关文章
|
2月前
|
数据可视化 Java API
Spring Boot与Swagger的集成
Spring Boot与Swagger的集成
|
2月前
|
Java API 开发者
在Spring Boot中集成Swagger API文档
在Spring Boot中集成Swagger API文档
|
1月前
|
Java API Spring
springboot集成swagger
这篇文章介绍了如何在Spring Boot项目中集成Swagger 2.10.0来生成API文档,包括添加依赖、编写配置类、创建接口文档,并使用Knife4j美化Swagger界面。
|
2月前
|
JSON 缓存 Java
Spring Boot集成 Swagger2 展现在线接口文档
本节课详细分析了 Swagger 的优点,以及 Spring Boot 如何集成 Swagger2,包括配置,相关注解的讲解,涉及到了实体类和接口类,以及如何使用。最后通过页面测试,体验了 Swagger 的强大之处,基本上是每个项目组中必备的工具之一,所以要掌握该工具的使用,也不难。
|
1月前
|
Java
SpringBoot 配置 Swagger
SpringBoot 配置 Swagger
26 0
|
2月前
|
Java 测试技术 API
|
3月前
|
安全 Java API
技术笔记:SpringBoot集成Swagger3.0(详细)
技术笔记:SpringBoot集成Swagger3.0(详细)
|
3月前
|
Java
springboot集成swagger2并分组全局设置Authorization
springboot集成swagger2并分组全局设置Authorization
104 0
|
4月前
|
XML Java API
Spring Boot中使用集成swagger-bootstrap-ui
Spring Boot中使用集成swagger-bootstrap-ui
121 0

热门文章

最新文章