springboot项目集成swagger管理API接口

1.引入swagger依赖
<!-- swagger -->
<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>
2.可以引入第三方管理界面

由于swagger原生API管理界面不是很好操作,所以可以借助第三方API接口管理工具
bootstrap-ui:

<!-- bootstrap-ui -->
<dependency>
	<groupId>com.github.xiaoymin</groupId>
	<artifactId>swagger-bootstrap-ui</artifactId>
	<version>1.9.3</version>
</dependency>
<dependency>
	<groupId>io.springfox</groupId>
	<artifactId>springfox-bean-validators</artifactId>
	<version>2.9.2</version>
</dependency>

knife4j:

<!-- knife4j -->
<dependency>
	<groupId>com.github.xiaoymin</groupId>
	<artifactId>knife4j-spring-boot-starter</artifactId>
	<version>2.0.2</version>
</dependency>
<dependency>
	<groupId>org.hibernate.validator</groupId>
	<artifactId>hibernate-validator</artifactId>
</dependency>

以上两个都是管理API接口比较好的工具,二者选其一就好

3.编写 Swagger2Config 配置类
@Configuration
@EnableSwagger2
public class Swagger2Config implements WebMvcConfigurer {

    /**
     * 展示该项目模块下所有api接口
     * @return
     */
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo()).groupName("全部")
                .select()
                // 为当前包路径
                .apis(RequestHandlerSelectors.basePackage("com.example.nosql")).paths(PathSelectors.any())
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .build();
    }

    /**
     * 展示该项目下指定模块的api接口,项目中的业务模块可直接添加配置并调用此方法
     * @param Apiname
     * @param ScanUrl
     * @return
     */
    public Docket scanningApi(String Apiname, String ScanUrl) {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo()).groupName(Apiname)
                .select()
                //此包路径下的类,才生成接口文档
                .apis(RequestHandlerSelectors.basePackage(ScanUrl))
                //加了ApiOperation注解的类,才生成接口文档
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                // 页面标题
                .title("SpringBoot使用Swagger2构建RESTful API")
                // 创建人信息
                .contact(new Contact("MrWang", "https://www.cnblogs.com/zs-notes/category/1258467.html", "1633639075@qq.com"))
                // 版本号
                .version("1.0")
                // 描述
                .description("后台API接口")
                .build();
    }

    @Bean
    public Docket createWgApi() {
        return this.scanningApi("用户操作", "com.example.nosql.mongo.controller");
    }
    @Bean
    public Docket createCrawlerApi() {
        return this.scanningApi("爬取数据", "com.example.nosql.elasticsearch.controller");
    }
}

如果使用 bootstrap-ui 管理界面,则在配置类上添加 @EnableSwaggerBootstrapUI
如果使用 knife4j 管理界面,则在配置类上添加 @EnableKnife4j 和 @Import(BeanValidatorPluginsConfiguration.class)

4.修改启动类

为了快速打开API接口管理界面,可以对启动类做一下修改

  • 在启动类上添加 @Slf4j 注解
  • 修改main方法
public static void main(String[] args) throws UnknownHostException {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(NosqlApplication.class, args);
        Environment environment = applicationContext.getEnvironment();
        String ip = InetAddress.getLocalHost().getHostAddress();
        String port = environment.getProperty("server.port");
        log.info("\n----------------------------------------------------------\n\t" +
                "Doc-ui: \t\thttp://" + ip + ":" + port + "/doc.html\n\t" +
                "Swagger-ui: \thttp://" + ip + ":" + port + "/swagger-ui.html\n" +
                "----------------------------------------------------------");
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值