Swagger框架使用

一、Swagger介绍:

  1. 是一款让你更好的书写API文档规范且完整的框架。
  2. 提供描述、生产、消费和可视化RESTful Web Service。
  3. 是由庞大工具集合支撑的形式化规范。这个集合涵盖了从终端用户接口、底层代码库到商业API管理的方方面面。

二、配置过程

1、在boot启动类加入注解@EnableSwagger2

@EnableSwagger2

2、在pom.xml文件中引入依赖

<!--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>
		<!--guava依赖包 高性能缓存组件-->
		<dependency>
			<groupId>com.google.guava</groupId>
			<artifactId>guava</artifactId>
			<version>25.1-jre</version>
		</dependency>

3、配置Swagger类

import org.springframework.context.annotation.Bean;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * Swgger 配置类
 */
public class SwggerConfig {
    /**
     * 创建API应用对象
     * 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现
     * @return
     */
    @Bean
    public Docket api(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //设置选择器,选择带Api接口类的类
                .select()
                //设置路径筛选 ,any表示不筛选
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();

    }

    /**
     * 创建该API的基本信息(这些基本信息会展现在文档页面中)
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("xxx系统")
                .description("这里是商城所有api文档")        //信息描述
                .termsOfServiceUrl("https://www.baidu.com")  //服务条款网址
                .build();
    }
}

4、拦截器中配置

//防止把默认的静态资源路径覆盖了,手动设置的方式
@Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        //   webjars/** 表示已jar包方式引入静态资源
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

三、其他注解介绍

  • @Api 注解在controller类上
  • @ApiModel 注解在类上,一般是实体类
  • @ApiOperation 注解在方法上,表明方法级解释
  • @ApiImplicitParams 注解在方法上,一般与@ApiImplicitParam共用,多个参数逗号隔开,表请求参数
  • @ApiResponses 注解在方法上表响应,一般与@ApiResponse公用

四、使用方法以及效果演示

1、swagger页面

配置完成之后在浏览器的地址栏输入http://localhost:8080/swagger-ui.html进行访问,可见如下页面:
在这里插入图片描述
2、登录功能演示

点击login登录功能
在这里插入图片描述
可见如下信息,证明登录成功
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值