1. 程式人生 > >Spring 4.2.2以上版本和swagger整合方案和踩過的坑

Spring 4.2.2以上版本和swagger整合方案和踩過的坑

因為公司使用的spring版本太高,在整合swagger的時候會存在一些問題,而網上的很多例項大多都是版本比較低的,為了使朋友們少踩坑,我這邊將整合的過程記錄一下:願意瞭解原始碼的朋友直接求求交流分享技術二一四七七七五六三三

1. 引入spring、swagger的相關jar包(springfox-swagger2、springfox-swagger-ui),在pom.xml中配置:

<dependency>  
            <groupId>io.springfox</groupId>  
            <artifactId>springfox-swagger2</artifactId>  
            <version>2.4.0</version>  
            <exclusions>  
                <exclusion>  
                    <groupId>org.springframework</groupId>  
                    <artifactId>spring-core</artifactId>  
                </exclusion>  
                <exclusion>  
                    <groupId>org.springframework</groupId>  
                    <artifactId>spring-beans</artifactId>  
                </exclusion>  
                <exclusion>  
                    <groupId>org.springframework</groupId>  
                    <artifactId>spring-context</artifactId>  
                </exclusion>  
                <exclusion>  
                    <groupId>org.springframework</groupId>  
                    <artifactId>spring-context-support</artifactId>  
                </exclusion>  
                <exclusion>  
                    <groupId>org.springframework</groupId>  
                    <artifactId>spring-aop</artifactId>  
                </exclusion>  
                <exclusion>  
                    <groupId>org.springframework</groupId>  
                    <artifactId>spring-tx</artifactId>  
                </exclusion>  
                <exclusion>  
                    <groupId>org.springframework</groupId>  
                    <artifactId>spring-orm</artifactId>  
                </exclusion>  
                <exclusion>  
                    <groupId>org.springframework</groupId>  
                    <artifactId>spring-jdbc</artifactId>  
                </exclusion>  
                <exclusion>  
                    <groupId>org.springframework</groupId>  
                    <artifactId>spring-web</artifactId>  
                </exclusion>  
                <exclusion>  
                    <groupId>org.springframework</groupId>  
                    <artifactId>spring-webmvc</artifactId>  
                </exclusion>  
                <exclusion>  
                    <groupId>org.springframework</groupId>  
                    <artifactId>spring-oxm</artifactId>  
                </exclusion>  
            </exclusions>  
        </dependency>  
        <dependency>  
            <groupId>io.springfox</groupId>  
            <artifactId>springfox-swagger-ui</artifactId>  
            <version>2.4.0</version>  
        </dependency> 

提醒: 特別注意,springfox-swagger2在整合的時候,已經引入了spring的相關jar,特別是spring-context、spring-context-support的版本和專案中使用的版本完全不一致,專案在啟動的時候出現很多包衝突的問題,這邊在引入pom.xml檔案的時候過濾掉了spring的相關jar包。

2. 編寫Swagger的配置類:

package com.ml.honghu.swagger.web;  
  
import org.springframework.context.annotation.Bean;  
import org.springframework.context.annotation.ComponentScan;  
import org.springframework.context.annotation.Configuration;  
import org.springframework.web.servlet.config.annotation.EnableWebMvc;  
  
import springfox.documentation.builders.ApiInfoBuilder;  
import springfox.documentation.builders.PathSelectors;  
import springfox.documentation.builders.RequestHandlerSelectors;  
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;  
  
@EnableWebMvc  
@EnableSwagger2  
@Configuration  
@ComponentScan(basePackages ={"com.ml.honghu.**.rest"})  
public class SwaggerConfig {  
    @Bean  
    public Docket createRestApi() {  
        return new Docket(DocumentationType.SWAGGER_2)  
                .apiInfo(apiInfo())  
                .select()  
                .apis(RequestHandlerSelectors.basePackage("com.ml.honghu"))  
                .paths(PathSelectors.any())  
                .build();  
    }  
  
    private ApiInfo apiInfo() {  
        return new ApiInfoBuilder()  
                .title("介面列表 v1.0")  
                .description("介面資訊")  
                .termsOfServiceUrl("http://honghu.com")  
                .contact(new Contact("", "", "HongHu"))  
                .version("1.1.0")  
                .build();  
    }  
}  

3. 在spring-mvc.xml檔案中進行過濾器的配置,過濾掉swagger的相關訪問配置:

<mvc:exclude-mapping path="/swagger*/**"/>  
<mvc:exclude-mapping path="/v2/**"/>  
<mvc:exclude-mapping path="/webjars/**"/> 

4. 服務配置項

@Api("區域服務")
@RestController  
@RequestMapping(value = "/rest/area")  
public class AreaService  {  
  
    @Autowired  
    private AreaService areaService;  
      
    <span style="color: #ff0000;">@ApiOperation(value = "區域列表", httpMethod = "GET", notes = "區域列表")</span>  
    @IsLogin  
    @ResponseBody  
    @RequestMapping(value = "treeData", method = RequestMethod.GET)  
    public List<Map<String, Object>> treeData(  
            <span style="color: #ff0000;">@ApiParam(required = true, value = "區域ID")</span>  @RequestParam(required=false) String extId, HttpServletResponse response) {  
        List<Map<String, Object>> mapList = Lists.newArrayList();  
        List<Area> list = areaService.findAll();  
        for (int i=0; i<list.size(); i++){  
            Area e = list.get(i);  
            if (StringUtils.isBlank(extId) || (extId!=null && !extId.equals(e.getId()) && e.getParentIds().indexOf(","+extId+",")==-1)){  
                Map<String, Object> map = Maps.newHashMap();  
                map.put("id", e.getId());  
                map.put("pId", e.getParentId());  
                map.put("name", e.getName());  
                mapList.add(map);  
            }  
        }  
        return mapList;  
    }  
}  

4. 啟動專案,檢視結果:

相關推薦

Java架構-Spring 4.2.2以上版本swagger整合方案

因為公司使用的spring版本太高,在整合swagger的時候會存在一些問題,而網上的很多例項大多都是版本比較低的,為了是朋友們少才坑,我這邊將整合的過程記錄一下: 引入spring、swagger的相關jar包(springfox-swagger2、springfox-sw

Spring 4.2.2以上版本swagger整合方案

因為公司使用的spring版本太高,在整合swagger的時候會存在一些問題,而網上的很多例項大多都是版本比較低的,為了使朋友們少踩坑,我這邊將整合的過程記錄一下:願意瞭解原始碼的朋友直接求求交流分享技術二一四七七七五六三三 1. 引入spring、swagger的相關ja

27.Spring-Boot中攔截器中靜態資源的處理()以及Spring mvc configuring拓展介紹

一.springboot中對靜態資源的處理  預設情況下,springboot提供存放放置靜態資源的資料夾:  /static  /public   /resources  /META-INF/resources 對於maven專案即就是存在src/main/re

TortoiseSVN 1.9.5安裝 與 Eclipse4.4.2以上版本中安裝SVN插件

new port 作用 nbsp fin 1.8 detail net .net 引自: http://blog.csdn.net/chenchunlin526/article/details/54631458 TortoiseSVN 1.9.5安裝 與 Eclipse4

TortoiseSVN 1.9.5安裝 與 Eclipse4.4.2以上版本中安裝SVN外掛

引自:http://blog.csdn.net/chenchunlin526/article/details/54631458 TortoiseSVN 1.9.5安裝 與 Eclipse4.4.2及以上版本中安裝SVN外掛  ---於2017-7-11編輯過 1、先在本機安裝TortoiseSVN 1.9.

spring (4.0.2)——(尚矽谷)學習筆記1

aspect 什麽 企業應用 周期 持久層 非侵入 mvc 註入 JD 1、Spring是什麽?   ①Spring 是一個開源框架;   ②Spring 為簡化企業級應用開發而生。使用Spring可以使簡單的JavaBean實現以前只有EJB才能實現的功能。   ③Spr

Elasticsearch 2.0以上版本根據條件批量刪除Java如何實現

Elasticsearch在2.0以前版本,刪除操作有兩種方式,一種是通過id來進行刪除,但是這種方式一般不常用,因為id不容易得到;另一種方式是通過先查詢操作,然後刪除,也就是通過client.prepareDeleteByQuery這種方式來根據條件批量刪除資料:

luabind 0.9.1在boost 1.49+gcc-4.6.3以上版本的編譯問題處理

將boost更新到1.53時, 發現luabind死活編譯不過, 報錯如下 error: missing binary operator before token "(" 根據老外的描述, boost中的BOOST_PP_ITERATION_FLAGS從1.49版本後發生了一些變化. 在git找到一個

struts2 2.5以上版本安裝中web.xml的配置(包括核心過濾器filter)

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-

從讀取視訊程式比較opencv1.0與2.0以上版本

opencv入門的那本經典教材《學習opencv》上關於讀取視訊程式碼是基於1.0版本寫的,筆者在2.4.9版本上改寫了一下。 //《學習opencv》基於1.0版本讀取視訊原始碼 #include"highgui.h" int main(int argc, char**argv) { cv

Apache2.2以上版本與Tomcat整合的好方法

今天技術總監叫實現Apache和Tomacat整合,在網上找了很多資料,結果發現還是這個最方便! 下面是實現2個tomcat實現負載均衡,如果只有一個則可刪除一個 apache2.2以上版本,無需使用jk_mod來整合tomcat,直接使用ajp,很方便。 修改ap

vue-cli工具2.9以上版本的使用

新版vue-cli使用基礎 第一步安裝 安裝和原來的一樣,執行命令,全域性安裝 npm install vue-cli -g 新建專案 vue init webpack Vue

精通Spring+4.x++企業開發與實踐之基於@AspectJSchema的AOP

#  精通Spring+4.x++企業開發與實踐之基於@AspectJ和Schema的AOP 使用@AspectJ的條件 1.保證是java5以上的版本(需要使用註解,而java5及以上才使用註解) 2.需要將Spring的asm(輕量級的位元組碼處理框架)的模組新增

spring boot1.5.X以上版本@ConfigurationProperties註解沒有location屬性後的替代用法

今天看springBoot書發現書中@ConfigurationProperties註解可以有location屬性,我始終調不出來,最後發現我用的是spring boot1.7 具體替代方案如下:

HttpClient使用詳解(4.3.X以上版本)

       Http協議的重要性相信不用我多說了,HttpClient相比傳統JDK自帶的URLConnection,增加了易用性和靈活性(具體區別,日後我們再討論),它不僅是客戶端傳送Http請求變得容易,而且也方便了開發人員測試介面(基於Http協議的),即提高了開發

IONICCordova安裝、打包

cordova 匹配 gis androi update glob 用戶名 info 註意 1、問題1:直接執行npm install -g cordova ionic,因為網絡原因,執行不成功 解決方案:將npm映射到淘寶服務器:npm install -g

03、Swagger2Springmvc整合詳細記錄(爬記錄)

component 效果 ges context tex ron 問題 package amp 時間 內容 備註 2018年6月18日 基本使用 spirngmvc整合swagger2 開始之前這個系列博文基本是,在項目的使用中一些模塊的內容記錄,但是

Asp.Net Core中使用Swagger,你不得不

action npr resolve com rate point creat 21點 use   很久不來寫blog了,換了新工作後很累,很忙。每天常態化加班到21點,偶爾還會到淩晨,加班很累,但這段時間,也確實學到了不少知識,今天這篇文章和大家分享一下:Asp.Net

RobotFrameworkEclipse整合-安裝使用說明

1、安裝python27的版本。 Python2與python3不衝突 ,可以都安裝,指定不同的目錄就好。 配置python2的環境變數,在python3的環境變數之前。 將所有的安裝操作完成之後,去掉python2的環境變數設定。 安裝注意事項,請看2之後的說

JfreeChartstruts2整合方案

      最近在忙一個專案,需要用到圖表,在網上Google了好多關於做圖表的框架,發現java陣營裡面,用的最多的就是JfreeChart了,開源並且免費,這是我技術選型的一貫要求,呵呵,折騰了這麼久,總結出了JFreeChart和struts2整合的方法,現在貢獻給大