SpringMVC集成swagger2,通过测试版本

一、pom引入jar

一定要引入springfox-swagger-ui,这样不需要手动下载swagger-ui放到项目中,方便多了。
swagger依赖jackson,guava

<!-- swagger 开始-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.4.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.4.0</version>
        </dependency>

        <dependency>  
            <groupId>com.fasterxml.jackson.core</groupId>  
            <artifactId>jackson-core</artifactId>  
            <version>2.8.0</version>  
        </dependency>  
        <dependency>  
            <groupId>com.fasterxml.jackson.core</groupId>  
            <artifactId>jackson-databind</artifactId>  
            <version>2.6.3</version>  
        </dependency>  
        <dependency>  
            <groupId>com.fasterxml.jackson.core</groupId>  
            <artifactId>jackson-annotations</artifactId>  
            <version>2.6.3</version>  
        </dependency> 
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>19.0</version>
        </dependency>
        <!-- swagger 结束-->

二、web.xml配置springmvc

<servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:/springMVC.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    <servlet-mapping>   
        <servlet-name>dispatcher</servlet-name>  
        <url-pattern>/</url-pattern>  
    </servlet-mapping>  

三、修改springmvc配置文件

<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/" />  
<mvc:resources mapping="/webjars/**"   location="classpath:/META-INF/resources/webjars/" />  

四、配置swagger

package com.wondertek.inter.controller;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import springfox.documentation.builders.ApiInfoBuilder;
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
@EnableSwagger2
public class SpringfoxConfig extends WebMvcConfigurerAdapter{

    @Bean  
    public Docket api() {  
        return new Docket(DocumentationType.SWAGGER_2)  
                .select()  
                .apis(RequestHandlerSelectors.any())  
                .build()  
                .apiInfo(apiInfo());  
    }  

    private ApiInfo apiInfo() {  
        return new ApiInfoBuilder()  
                .title("xxxAPI 文档")  
                .description("HTTP对外开放接口")  
                .version("1.0.0")  
                .termsOfServiceUrl("http://xxx.xxx.com")  
                .license("LICENSE")  
                .licenseUrl("http://xxx.xxx.com")  
                .build();  
    }  

}

四、访问swagger-ui

http://localhost:8080/项目路径/swagger-ui.html

猜你喜欢

转载自blog.csdn.net/chuxue1989/article/details/80733280