springboot-swagger配置以及我的报错(贼详细)

本文介绍了如何在Spring Boot项目中引入并配置Swagger2和Swagger3,包括添加依赖、配置Swagger信息以及访问Swagger UI。通过Docket bean定制API信息,并展示了不同版本Swagger的访问URL。此外,还讨论了在不同环境下启用Swagger以及如何根据包路径自动扫描接口。
摘要由CSDN通过智能技术生成

Swagger使用

导入依赖

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>3.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>3.0.0</version>
</dependency>
<!--如果报错再导入下面依赖-->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>
<!--如果还报错再改springboot版本-->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.5.6</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

Swagger装配

import org.springframework.context.annotation.Configuration;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration //加入配置
@EnableSwagger2//开启swagger
@EnableOpenApi //开启api
public class SwaggerConfig {


}

SwaggerAPI页面访问

注意点

  • 因为装配的Swagger是3.0.0的版本,所以访问的网页有了变化
    网页地址为:http://localhost:8080/swagger-ui/index.html
  • 如果是3.0.0以下版本
    网页地址为:http://localhost:8080/swagger-ui.html

配置Swagger信息

重写,代替默认的Swagger实例 Docket。

CTRL点击Docket,发现需要一个文本类型类。
在这里插入图片描述
CTRL点击DocumentationType,探究这个玩意
在这里插入图片描述
发现有个SWAGGER_2的静态最终类,可以点出来。
又发现Docket还有一个ApiInfo的属性,它可以指定一些信息。
基本配置如下:

@Configuration
@EnableSwagger2
@EnableOpenApi
public class SwaggerConfig {

    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
        		.apiInfo(apiInfo())
                .select()
                //配置自动扫描包获取接口
   			    .apis(RequestHandlerSelectors.basePackage("com.zhuli.controller"))
                .build();
    }

    public ApiInfo apiInfo(){
        return new ApiInfo(
                "my lover is zyp",
                "zhuli",
                "1.1",
                "urn:tos",
                new Contact("朱溧", "江苏省泰州市", "2122332078@qq.com"),
                "zhuli 2.0",
                "阿巴阿巴",
                new ArrayList<>()//这个地方我也不晓得啥东西
                );

    }

}

比较

原先swagger的网页:
在这里插入图片描述
配置后:
在这里插入图片描述
这里的groupname可以指定组名
在这里插入图片描述
当有多个开发人员时,可以装配多个Docket。
对于不同环境是否需要swagger在下面也有显示。
别用错包

package com.zhuli.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
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.*;
@Configuration
@EnableSwagger2
@EnableOpenApi
public class SwaggerConfig {
    @Bean
    public Docket docket(Environment environment){
        //环境名是否在of参数里
        Profiles dev = Profiles.of("dev");
        boolean b = environment.acceptsProfiles(dev);
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(b)//判断环境是否开启swagger
                .groupName("朱溧") //分组组名
                .select()
                //配置自动扫描包获取接口
                .apis(RequestHandlerSelectors.basePackage("com.zhuli.controller"))
                .build();
    }

    public ApiInfo apiInfo(){
        return new ApiInfo(
                "my lover is zyp",
                "zhuli",
                "1.1",
                "urn:tos",
                new Contact("朱溧", "江苏省泰州市", "2122332078@qq.com"),
                "zhuli 2.0",
                "阿巴阿巴",
                new ArrayList<>()//这个地方我也不晓得啥东西
                );

    }

}

对比学习!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

这猪能飞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值