SpringBoot项目集成Swagger

本文介绍了在Spring项目中集成Swagger2的步骤。首先在pom文件加入相关依赖,接着在Application入口类加上注解@EnableSwagger2,然后创建swagger配置类,最后在controller类中使用swagger标签,屏蔽部分spring - boot自带接口并设置主界面信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.pom文件加入依赖:

          <!-- Swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>

2.Application入口类加上注解@EnableSwagger2

 

2.创建swagger配置类:

package com.**.knot.conf;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.google.common.base.Predicates;

import springfox.documentation.builders.ApiInfoBuilder;
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;

@Configuration
@EnableSwagger2
public class Swagger2Config {

  /**
   * 屏蔽部分的spring-boot自带的接口
   * 
   * @return
   */
  @Bean

  public Docket createRestApi() {
    return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.any())
        .paths(Predicates.not(PathSelectors.regex("/error.*"))).paths(Predicates.not(PathSelectors.regex("/info.*")))
        .paths(Predicates.not(PathSelectors.regex("/autoconfig.*")))
        .paths(Predicates.not(PathSelectors.regex("/health.*")))
        .paths(Predicates.not(PathSelectors.regex("/metrics.*")))
        .paths(Predicates.not(PathSelectors.regex("/mappings.*")))
        .paths(Predicates.not(PathSelectors.regex("/trace.*")))
        .paths(Predicates.not(PathSelectors.regex("/configprops.*")))
        .paths(Predicates.not(PathSelectors.regex("/beans.*"))).paths(Predicates.not(PathSelectors.regex("/env.*")))
        .paths(Predicates.not(PathSelectors.regex("/dump.*")))
        .paths(Predicates.not(PathSelectors.regex("/auditevents.*")))
        .paths(Predicates.not(PathSelectors.regex("/docs.*"))).paths(Predicates.not(PathSelectors.regex("/archaius.*")))
        .paths(Predicates.not(PathSelectors.regex("/features.*")))
        .paths(Predicates.not(PathSelectors.regex("/pause.*"))).paths(Predicates.not(PathSelectors.regex("/refresh.*")))
        .paths(Predicates.not(PathSelectors.regex("/resume.*")))
        .paths(Predicates.not(PathSelectors.regex("/actuator.*")))
        .paths(Predicates.not(PathSelectors.regex("/jolokia.*")))
        .paths(Predicates.not(PathSelectors.regex("/loggers.*")))
        .paths(Predicates.not(PathSelectors.regex("/restart.*")))
        .paths(Predicates.not(PathSelectors.regex("/service-registry/instance-status.*")))
        .paths(Predicates.not(PathSelectors.regex("/logfile.*")))
        .paths(Predicates.not(PathSelectors.regex("/channels.*"))).paths(Predicates.not(PathSelectors.regex("/bus.*")))
        .paths(Predicates.not(PathSelectors.regex("/heapdump.*"))).build();
  }

  /**
   * Swagger2主界面信息,描述api的基本信息用于展示
   * 
   * @return
   */
  private ApiInfo apiInfo() {
    return new ApiInfoBuilder().title("knot服务接口定义和规范").description("knot服务接口定义和规范")
        .termsOfServiceUrl("http://****.com/").contact(new Contact("jjzhao@****.com", "", "")).version("1.0")
        .license("版权所有 ©***信息科技有限公司").build();
  }

}
 

 

3.在controller类中使用swagger标签:

 

 

4.最终效果:

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值