Swagger常用的方法及配置流程

Swagger

前言

使用Swagger的好处

1.RestFul Api 文档在线自动生成工具=>Api文档与API定义同步更新。
2.可以直接运行,在线测试API接口。
3.支持多种语言。

官网

http://swagger.io/

使用的具体东西

在项目中使用Swagger需要springbox;
swagger2
ui

配置Swagger

1.导入Swagger的Jar包(配置Maven)

    <dependency>
    <!--添加Swagger依赖 -->
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.7.0</version>
    </dependency>
    <dependency>
        <!--添加Swagger-UI依赖 -->
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.7.0</version>
    </dependency>

2.配置Swagger的Config

在config组里创建SwaggerCofig类,
通过注解
@Configuration
@EnableSwagger2
开启,注意第一个注解只是单纯的@Component的意思,第二个@EnableSwagger2是开启了Swagger2。
package com.jic.myspringboot.Config;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
}

3.测试

开启后访问localhost:8080/swagger-ui.html

4.配置Swagger类的bean实例Docket及apiInfo

package com.jic.myspringboot.Config;


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

import java.util.ArrayList;

import static springfox.documentation.service.ApiInfo.DEFAULT_CONTACT;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
//配置了Swagger的bean实例;
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo());
}
private ApiInfo apiInfo(){

Contact contact=new Contact("李贺雲","https://me.csdn.net/Gakooon","1034108904@qq.com");
return new ApiInfo("公司员工体检系统"
   , "practice demo"
   , "1.0"
   , "https://me.csdn.net/Gakooon"
   , contact
   , "Apache 2.0"
   , "http://www.apache.org/licenses/LICENSE-2.0"
, new ArrayList());
}
}

4.配置扫描接口

@Configuration
@EnableSwagger2
public class SwaggerConfig {
//配置了Swagger的bean实例;
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
//在这后面可以设置是否启动,如果不启动就把.enabled(false)
.select()
//   RequestHandlerSelectors  配置要扫描接口的方式。
//.basePackage("com.jic.myspringboot.controller") 扫描指定的包  主流方法
//.any() 扫描全部
//.none() 不扫描
//.withClassAnnotation(RestController.class) 扫描类上的注解
//.withMethodAnnotation(GetMapping.class)
.apis(RequestHandlerSelectors.basePackage("com.jic.myspringboot"))
//.paths(PathSelectors.ant("/Person/**")) 过滤作用,设置条件
.paths(PathSelectors.ant("/Person/**"))
.build();
}
private ApiInfo apiInfo(){

Contact contact=new Contact("李贺雲","https://me.csdn.net/Gakooon","1034108904@qq.com");
return new ApiInfo("公司员工体检系统"
   , "practice demo"
   , "1.0"
   , "https://me.csdn.net/Gakooon"
   , contact
   , "Apache 2.0"
   , "http://www.apache.org/licenses/LICENSE-2.0"
, new ArrayList());
}
}

6.配置项目环境

7.常用的注解

@ApiModel("")放在最前头,给类加上注释。
@ApiModelProperty("")给实体类加上注释方便人家看懂
@ApiOperation("")在方法上放一个注释,来解释方法
@ApiParm("")在传参前面,注释参数

8.利用Swagger来测试

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值