[Swagger] Spring MVC 組件配置 之 Swagger整合

小編:啊南 59閱讀 2020.11.19

Spring MVC 組件配置 之 Swagger整合(自定義樣式調整)(springmvc-swagger)
- swagger靜態資源和SpringMVC項目整合
- 支持自定義樣式開發
- 基于swagger2.2.10版本開發
開發環境
- tomcat 7.0.78
- jdk 1.7+
- spring 4.3.13.RELEASE
SwaggerConfig 配置類
package cn.com.showclear.config;import com.mangofactory.swagger.configuration.SpringSwaggerConfig;
import com.mangofactory.swagger.models.dto.ApiInfo;
import com.mangofactory.swagger.plugin.EnableSwagger;
import com.mangofactory.swagger.plugin.SwaggerSpringMvcPlugin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;/**
* Swagger初始化配置文件
* @author YF-XIACHAOYANG
* @date 2017/12/26 10:13
*/
@EnableWebMvc
@EnableSwagger
@ComponentScan(basePackages = "cn.com.showclear.activiti.controller.data")
public class SwaggerConfig {   private SpringSwaggerConfig springSwaggerConfig;   /**
    * Required to autowire SpringSwaggerConfig
    */
   @Autowired
   public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig)
   {
       this.springSwaggerConfig = springSwaggerConfig;
   }   /**
    * Every SwaggerSpringMvcPlugin bean is picked up by the swagger-mvc
    * framework - allowing for multiple swagger groups i.e. same code base
    * multiple swagger resource listings.
    */
   @Bean
   public SwaggerSpringMvcPlugin customImplementation()
   {
       return new SwaggerSpringMvcPlugin(this.springSwaggerConfig)
               .apiInfo(apiInfo())
               .includePatterns(".*?");
   }   private ApiInfo apiInfo()
   {
       ApiInfo apiInfo = new ApiInfo(
               "Scooper Activiti REST-API",
               "工作流后臺接口測試",
               "My Apps API terms of service",
               "xiazhaoyang@live.com",
               "web app",
               "My Apps API License URL");
       return apiInfo;
   }
}
spring-mvc.xml
<!-- 將 springSwaggerConfig加載到spring容器 -->
<bean class="com.mangofactory.swagger.configuration.SpringSwaggerConfig" />
<!-- 將自定義的swagger配置類加載到spring容器 -->
<bean class="cn.com.showclear.config.SwaggerConfig" />
<!-- don't handle the static resource -->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
spring-servlet.xml
 <!-- 靜態資源文件映射,不會被Spring MVC攔截 -->
<mvc:resources mapping="/swagger/**" location="/swagger/" />
web.xml
<!-- 配置Spring核心控制器 -->
<servlet>
   <servlet-name>springmvc</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <!--可以不配置.默認所對應的配置文件是WEB-INF下的{servlet-name}-servlet.xml,這里便是:spring-servlet.xml
   -->
   <init-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>classpath:/config/spring/spring-servlet.xml</param-value>
   </init-param>
   <load-on-startup>1</load-on-startup>
</servlet>
<!--這里可以用 / 但不能用 /*,攔截了所有請求會導致靜態資源無法訪問,所以要在spring-servlet.xml中配置mvc:resources
   -->
<servlet-mapping>
   <servlet-name>springmvc</servlet-name>
   <url-pattern>/</url-pattern>
</servlet-mapping>
index.html
/*添加樣式*/
<style type="text/css">
.hide{
 display:none;
}
</style>
/*調整路徑*/
url = "/scooper-activiti/api-docs";
/*隱藏頭部*/
<div id='header' class="hide">
maven 依賴
<!--swagger-->
<dependency>
   <groupId>com.mangofactory</groupId>
   <artifactId>swagger-springmvc</artifactId>
   <version>0.9.5</version>
</dependency><dependency>
   <groupId>com.fasterxml.jackson.core</groupId>
   <artifactId>jackson-annotations</artifactId>
   <version>2.4.4</version>
</dependency><dependency>
   <groupId>com.fasterxml.jackson.core</groupId>
   <artifactId>jackson-databind</artifactId>
   <version>2.4.4</version>
</dependency><dependency>
   <groupId>com.fasterxml.jackson.core</groupId>
   <artifactId>jackson-core</artifactId>
   <version>2.8.9</version>
</dependency>
1、swagger整合
https://www.cnblogs.com/jtlgb/p/6734177.html
https://www.2cto.com/kf/201604/499072.html
http://blog.csdn.net/hayre/article/details/51027201
http://www.mamicode.com/info-detail-525592.html2、Can't read swagger JSON from http...
http://blog.csdn.net/shecanwin/article/details/55667102
http://blog.csdn.net/xyw591238/article/details/519391113、No qualifying bean of type 'com.mangofactory.swagger.configuration.SpringSwaggerConfig' available: ...
https://www.cnblogs.com/driftsky/p/4952918.html4、swagger文件上傳的寫法
http://blog.csdn.net/qq_23167527/article/details/78559096
swagger靜態資源文件下載
1、版本下載列表
https://github.com/Yiyuery/swagger-ui2、2.2.10下載鏈接
https://github.com/swagger-api/swagger-ui/tree/v2.2.10

注意:swagger 版本選擇 2.0+ 版本 [2.2.10] 

關聯標簽:
华东15选5彩票奖结果 新疆18选7开奖记录 台湾棒球比分 挖矿速度表 莱特币 幸运农场计划网 快乐12彩 iphone捕鱼达人 唐人游济南麻将下载 双色球走势图历史记录 北京赛车pk拾平台出租 比特币现金交易平台 全民麻将内购破解版下载 p5复式投注 竞彩篮球胜分差技巧 亚冠足彩比分推荐 腾讯欢乐捕鱼 重庆时时彩开奖结果app