springBoot整合swagger-bootstrap-ui实现接口文档的生成查看功能

前言

在开发完rest资源项目后,我们一般要提供接口文档作为接口调用的说名,每次手动维护接口文档的工作会和繁琐,所以使用swagger-bootstrap-ui来实现项目接口的为维护

Swagger 常用注解使用详解

加入maven的依赖

在pom.xml文件中添加

<!-- swagger2 和 swagger2 依赖 -->
<dependency>
	<groupId>io.springfox</groupId>
	<artifactId>springfox-swagger2</artifactId>
	<version>2.9.2</version>
</dependency>
<dependency>
	<groupId>com.github.xiaoymin</groupId>
	<artifactId>swagger-bootstrap-ui</artifactId>
	<version>1.9.6</version>
</dependency>

Swagger的配置

package com.kcsm.product.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.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;

/**
 * 描述:Swagger的配置
 *
 * @Auther: lzx
 * @Date: 2019/9/23 09:11
 */
@Configuration
@EnableSwagger2
/*
接口文档的作用范围 -- 只在开发环境使用 等,下面的值何配置中定义的spring.profiles.active有/*关
 */
@Profile({"local","dev"})
public class SwaggerConfiguration {

    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //扫描的包
                .apis(RequestHandlerSelectors.basePackage("com.xxx.xxx"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("商品服务接口")
                .description("商品服务接口")
                .termsOfServiceUrl("http://www.xxx.cn/xxx/")
                .contact(new Contact("lzx","http://www.xxx.cn","lzx@qq.com"))
                //版本
                .version("1.0")
                .build();
    }

}

配置文件

在application.properties或application.yml配置中加入环境标签声明

# 读取了配置
spring.profiles.active=${SPRING_PROFILE}

启动项目

启动项目,访问 :项目地址/doc.html 可以看到如下界面
在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值