环境 : Maven 工程 , 已具备web项目的基本配置(包扫描之类)

1、引用swagger 的 依赖 :

      

  1. <dependency>  
  2.             <groupId>io.springfox</groupId>  
  3.             <artifactId>springfox-swagger2</artifactId>  
  4.             <version>2.4.0</version>  
  5.         </dependency>  
  6.         <dependency>  
  7.             <groupId>io.springfox</groupId>  
  8.             <artifactId>springfox-swagger-ui</artifactId>  
  9.             <version>2.4.0</version>  
  10.         </dependency>  

 

2、定义自己的Docket类 : MultiDocket

    

import java.util.List;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.SecurityScheme;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

public class MultiDocket extends Docket {
/** 认证信息 .*/
public void setSecurityScheme(List<? extends SecurityScheme> apiKey) {
this.securitySchemes(apiKey);
}
/** swagger的基本信息*/
public void setApiInfo(ApiInfo apiInfo) {
this.apiInfo(apiInfo);
}
/** swagger 界面显示的分组*/
public void setGroup(String group) {
this.groupName(group);
}
/** swagger 界面显示的url*/
public void setAntPath(String path) {
this.select().paths(PathSelectors.ant(path)).build();
}

public void setRegexPath(String path) {
this.select().paths(PathSelectors.regex(path)).build();
}
  /** 构造方法*/
public MultiDocket(DocumentationType documentationType) {
super(documentationType);
}
}

注 :
SecurityScheme 抽象类有3个实现  : ApiKey , BasicAuth ,OAuth , 本次博客没有OAuth的环境,因此采用 ApiKey 方法


3、自定义Swagger类 : CSwaggerConfig

import com.globalroam.core.spring.config.MultiDocket;
import java.util.List;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class CSwaggerConfig {
public CSwaggerConfig() {
}

public void setMultiDockets(List<MultiDocket> multiDockets) {
Docket docket = (Docket)multiDockets;
}
}
@EnableSwagger2 :注入swagger的注解
@Configuration:该类自动注入的注解


4、配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">

<!-- docket1 -->
<bean />
    </bean>

<!-- api info -->
<bean />
</bean>

  <!-- API key-->
<util:list />
</bean>
</util:list>



</beans>
























相关文章: