Swagger整合Stringboot配置

Swagger结合Stringboot配置

  • 引入依赖
<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>swagger-bootstrap-ui</artifactId>
    <version>1.9.6</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>3.0.0</version>
</dependency>
  • 编写配置类
package com.example.swagger.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
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;

/**
 * @author renzhengxuan
 * @date 2021/5/15
 */
@Configuration
@EnableSwagger2  //开启swagger的注解
public class SwaggerConfig {

    //这里时配置了两个组,如果配置一个组.groupName可以不写,但是配置多个组必须要写.groupName,以区别组名
    @Bean
    public Docket docket1(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("测试1")
                .select()
                //指定扫描的包com.example.swagger.test2
                .apis(RequestHandlerSelectors.basePackage("com.example.swagger.test1"))
                .build();
    }

    @Bean
    public Docket docket2(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("测试2")
                .select()
                //指定扫描的包com.example.swagger.test2
                .apis(RequestHandlerSelectors.basePackage("com.example.swagger.test2"))
                .build();
    }
    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("测试后台api文档")
                .description("后台api")
                //这个contact可写可不写
                .contact(new Contact(null,null,null))
                .version("V1.0")
                .build();
    }
}
  • 在这里配置好以后,启动项目,在浏览器输入(我是在本地测试)

  • localhost:8080/doc.html 出现如下界面,swagger就是配置成功了
    在这里插入图片描述

  • 此文章是记录遇到的问题,方便以后的查看,希望能够帮助到更多的人

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值