SpringMVC集成swagger2,通过测试版本

一、pom引入jar

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


        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger2artifactId>
            <version>2.4.0version>
        dependency>
        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger-uiartifactId>
            <version>2.4.0version>
        dependency>

        <dependency>  
            <groupId>com.fasterxml.jackson.coregroupId>  
            <artifactId>jackson-coreartifactId>  
            <version>2.8.0version>  
        dependency>  
        <dependency>  
            <groupId>com.fasterxml.jackson.coregroupId>  
            <artifactId>jackson-databindartifactId>  
            <version>2.6.3version>  
        dependency>  
        <dependency>  
            <groupId>com.fasterxml.jackson.coregroupId>  
            <artifactId>jackson-annotationsartifactId>  
            <version>2.6.3version>  
        dependency> 
        <dependency>
            <groupId>com.google.guavagroupId>
            <artifactId>guavaartifactId>
            <version>19.0version>
        dependency>
        

二、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

你可能感兴趣的:(SpringMVC)