maven项目中swagger实例

swagger引入依赖

		<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
  		<dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
             <version>2.7.0</version>
        </dependency>
         <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
		<dependency>
		    <groupId>io.springfox</groupId>
		    <artifactId>springfox-swagger-ui</artifactId>
		    <version>2.9.2</version>
		</dependency>

引入Swagger2配置信息/工具包

package com.hp.idea.common;

import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
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;

/**
 * Swagger2配置信息
 */
@Configuration  
@EnableSwagger2 	//开启swagger2。
public class Swagger2Config {

    @Bean
    public Docket webApiConfig(){

        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("webApi")
                .apiInfo(webApiInfo())
                .select()
                //只显示api路径下的页面
                .paths(Predicates.and(PathSelectors.regex("/api/.*")))
                .build();
    }
    @Bean
    public Docket adminApiConfig(){

        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("adminApi")
                .apiInfo(adminApiInfo())
                .select()
                //只显示admin路径下的页面
                .paths(Predicates.and(PathSelectors.regex("/hosp/.*")))
                .build();
    }
    private ApiInfo webApiInfo(){
       
        return new ApiInfoBuilder()
                .title("网站-API文档")
                .description("本文档描述了网站微服务接口定义")
                .version("1.0")
                .contact(new Contact("hp", "www.baidu.com", "XXxxxxx@qq.com"))
                .build();
    }
    private ApiInfo adminApiInfo(){
       
        return new ApiInfoBuilder()
                .title("后台管理系统-API文档")
                .description("本文档描述了后台管理系统微服务接口定义")
                .version("1.0")
                .contact(new Contact("hp", "www.baidu.com", "XXxxxxx@qq.com"))
                .build();
    }
}

Docket 传参DocumentationType

Docket 的构造函数需要一个 DocumentationType 作为参数,DocumentationType 的源码

	public static final springfox.documentation.spi.DocumentationType SWAGGER_12;
    public static final springfox.documentation.spi.DocumentationType SWAGGER_2;
    public static final springfox.documentation.spi.DocumentationType SPRING_WEB;

可以看到,它提供了三个构造好的 DocumentationType 常量,设置了使用 Swagger 哪个版本。

ApiInfo

 	public static final springfox.documentation.service.ApiInfo DEFAULT;
    private final java.lang.String version;
    private final java.lang.String title;
    private final java.lang.String description;
    private final java.lang.String termsOfServiceUrl;
    private final java.lang.String license;
    private final java.lang.String licenseUrl;
    private final springfox.documentation.service.Contact contact;
    private final java.util.List<springfox.documentation.service.VendorExtension> vendorExtensions;
  1. version:API 版本
  2. title:文档标题
  3. description:文档描述
  4. termsOfServiceUrl:团队链接
  5. license:许可
  6. licenseUrl:许可链接
  7. contact:联系人信息
  8. vendorExtensions:扩展信息
  9. DEFAULT:它就是我们在不做任何配置下启动 Swagger 显示的基本信息
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Too_Soup_Soup

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值