SpringMVCはswagger 2を統合し、バージョンをテストした.

13262 ワード

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