Spring mvc集成Swagger2

1.導(dǎo)入mavan相關(guān)資源

        <!-- swagger-springmvc dependencies -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.6.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.6.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.8.9</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.8.9</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.8.9</version>
        </dependency>

2.編寫SwaggerConfig類,注意包名換成自己的

package com.sansence.wine.swagger;
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 org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
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;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration    // 配置注解,自動(dòng)在本類上下文加載一些環(huán)境變量信息
@EnableSwagger2   // 使swagger2生效
@EnableWebMvc
@ComponentScan(basePackages = {"com.sansence.wine"})  //需要掃描的包路徑
public class SwaggerConfig extends WebMvcConfigurationSupport {

    @Bean
    public Docket swaggerSpringMvcPlugin() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("business-api")
                .select()   // 選擇那些路徑和api會(huì)生成document
                .apis(RequestHandlerSelectors.basePackage("com.sansence.wine"))
                .paths(PathSelectors.any())
                //.apis(RequestHandlerSelectors.any())  // 對(duì)所有api進(jìn)行監(jiān)控
                //.paths(PathSelectors.any())   // 對(duì)所有路徑進(jìn)行監(jiān)控
                .build();
    }



    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring 中使用Swagger2構(gòu)建RESTful API")
                .termsOfServiceUrl("http://blog.csdn.net/yangshijin1988")
                .description("此API提供接口調(diào)用")
                .license("License Version 2.0")
                .licenseUrl("http://blog.csdn.net/yangshijin1988")
                .version("2.0").build();
    }
}

3.將上面的類注入springmvc

<bean class="com.sansence.wine.swagger.SwaggerConfig" />

4.開啟注解

      <!-- 開啟注解 -->
    <mvc:annotation-driven/>
    <mvc:default-servlet-handler />

5.web.xml加入該代碼

 <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/v2/api-docs</url-pattern>
  </servlet-mapping>

6、使用Swagger UI模板
可在Swagger官網(wǎng)下載。地址:https://github.com/swagger-api/swagger-ui
下載完成后將swagger-ui下的dist目錄下的模板放入項(xiàng)目中,如在項(xiàng)目web-app下新建swagger放swagger-ui模板。
在spring-mvc中配置swagger文件夾自動(dòng)過濾。
<mvc:resources mapping="/swagger/**" location="/swagger/" cache-period="31556926" />
將index.html或swagger-ui.html文件js中的url換成url ="http://localhost:8080/project/v2/api-docs?group=business-api";

7.訪問 http://localhost:8080/project/swagger-ui.html

image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,715評(píng)論 19 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,290評(píng)論 6 342
  • 最近差點(diǎn)被公司開了,然后好心的hr問我愿不愿意換崗位,換崗位就可以留下來,我想了想,看看什么崗位吧,我靠,java...
    編城的南墻閱讀 1,345評(píng)論 1 1
  • 癥狀 按照該有的教程都配置完成了,swagger頁面也正常顯示,但是呢,頁面里面一個(gè)API也沒有,關(guān)鍵是我明明按照...
    Coselding閱讀 15,000評(píng)論 1 12
  • 一直以為,學(xué)習(xí)一門語言最快的辦法就是項(xiàng)目實(shí)踐,帶有目的性的去搜尋學(xué)習(xí)資料比從頭開始效率高得多。一直都有學(xué)pytho...
    Rockelbel閱讀 312評(píng)論 0 1

友情鏈接更多精彩內(nèi)容