欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Springboot配置Swagger的两种方式

程序员文章站 2022-06-23 10:43:30
Spring boot整合Swagger2一、方式一:使用配置文件来配置Swagger设置1.在pom.xml文件添加依赖 com.spring4all swagger-spring-boot-starter 1.8...

Spring boot整合Swagger2

一、方式1:使用配置文件来配置Swagger设置

1.在pom.xml文件添加依赖

        <dependency>
            <groupId>com.spring4all</groupId>
            <artifactId>swagger-spring-boot-starter</artifactId>
            <version>1.8.0.RELEASE</version>
        </dependency>

2.在配置文件application.yml添加配置

swagger:
  base-package: 'com.lzr.test.controller'
  base-path: '/**'
  title: '注解方式的Swagger'
  description: '这是注解方式的Swagger'
  version: '2.0'
  license-url: 'https://www.apache.org/licenses/LICENSE-2.0.html'
  license: 'The Apache License'
  contact:
    name: '吕小白'
    url: 'https:www.baidu.com'
    email: '******@qq.com'

3.在启动类添加注解@EnableSwagger2Doc

@EnableSwagger2Doc
@SpringBootApplication
public class TestApplication {

    public static void main(String[] args) {
        Object o=new Object();
        SpringApplication.run(TestApplication.class, args);
    }

}

4.如果报这个异常

Springboot配置Swagger的两种方式

5.在pom.xml添加如下依赖


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>

二、方式2:创建Java类来配置Swagger设置

1.在pom.xml添加依赖

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
<!--        导入swagger-ui依赖-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

2.创建Swagger的Java配置类

package com.lzr.test.utils;
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.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
/**
 * @Author 吕小白
 * @ClassName SwaggerUtil
 * @Date 2021/1/18 15:51
 * @Version 1.0
 **/
@Configuration
public class SwaggerUtil {
    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .pathMapping("/")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.lzr.test.controller"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(new ApiInfoBuilder()
                .title("医疗系统")
                        .description("用于管理患者信息的医疗系统平台")
                        .version("1.0")
                        .contact(new Contact("吕小白","https:www.baidu.com","****@qq.com")
                        )
                        .license("The Apache License")
                        .licenseUrl("https:www.baidu.com")
                        .build());
    }
}

3.在启动类添加注解@EnableSwagger2

@EnableSwagger2
@SpringBootApplication
@MapperScan(basePackages = "com.lzr.test.mapper")
public class TestApplication {

    public static void main(String[] args) {
        Object o=new Object();
        SpringApplication.run(TestApplication.class, args);
    }

}

总结:然后启动项目,在浏览器输入:http://localhost:8080/swagger-ui.html

Springboot配置Swagger的两种方式

本文地址:https://blog.csdn.net/Sponge_boy/article/details/112799060