SpringBoot集成Swagger

SpringBoot集成Swagger

1、建立一个SpringBoot项目
2、导入依赖

 <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

3、编写swagger配置类

@Configuration
@EnableSwagger2//开启Swagger2的自动配置
public class SwaggerConfig {
   
}

4、打开网页http://localhost:8080/swagger-ui.html,可以看见swagger网页则成功。

配置Swagger

1、通过配置Swagger的实例Docket来配置Swagger

  //Swagger实例Bean是Docket,所以通过配置Docket实例来配置Swagger。
    @Bean
    public Docket docket() {
   
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo());
    }

2、配置文档信息

 //配置Swagger信息
    private ApiInfo apiInfo(){
   

        Contact contact = new Contact("xhj", "www.xxx.com", "xxxxx.qq.com");
        return new ApiInfo( "title","description","version","termsOfServiceUrl",contact,"license","licenseUrl",new ArrayList());

    }

由于源码中只有一个构造方法,没有set方法,所以只能通过构造方法配置信息。

Swagger配置扫描接口

1、通过select方法扫描接口

 @Bean
    public Docket docket() {
   
        //any扫描项目中的所有接口
        //none不扫描接口
        //withMethodAnnotation(final Class<? extends Annotation> annotation),通过类上的注解扫描,如.withClassAnnotation(Controller.class)只扫描有 controller注解的类中的接口
        //basePackage(final String basePackage),根据包路径扫描接口
        //regex(final String pathRegex),通过正则表达式控制
        //ant(final String antPattern),通过ant(
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值