swagger3 和knif4j

1.导入依赖

 <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
            <version>3.0.2</version>
        </dependency>

2.书写配置类

package net.royalreairman.synth.commons.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.Contact;
import springfox.documentation.service.SecurityScheme;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.Collections;
import java.util.List;
@Profile({"local", "dev"})
@EnableOpenApi
@Configuration
public class SwaggerConfig {
    @Bean
    public Docket desertsApi() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("net.royalreairman.synth"))
                .paths(PathSelectors.any())
                .build()
                .groupName("desertsGroup")
                // 设置安全方案
                .securitySchemes(security())
                .enable(true);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Swagger3接口文档")
                .description("文档描述信息")
                .contact(new Contact("royal", "#", ""))
                .version("1.0")
                .build();
    }

    private List<SecurityScheme> security() {
        return Collections.singletonList(new ApiKey("Access-Token", "Access-Token", "header"));
    }
}

3.在启动类加上 接口信息 ,方便后期找到swagger地址

   private static final Logger logger = LoggerFactory.getLogger(SystemApplication.class);
  public static void main(String[] args) {
//    SpringApplication.run(SystemApplication.class, args);
      ConfigurableApplicationContext applicationContext = SpringApplication.run(SystemApplication.class,args);
      ConfigurableEnvironment environment = applicationContext.getEnvironment();
      String serverPort = environment.getProperty("server.port");

      String url = "http://127.0.0.1:" + serverPort + "/doc.html";
      logger.info("项目启动成功,knife4jsSwagger访问地址是:{}", url);
      url = "http://127.0.0.1:" + serverPort + "/swagger-ui/index.html#";
      logger.info("项目启动成功,swagger访问地址是:{}", url);
  }

4,启动项目

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Royalreairman

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

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

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

打赏作者

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

抵扣说明:

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

余额充值