springmvc4集成swagger2

首先在原有的springmvc工程的pom文件中增加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>

增加swagger的配置类

package com.founder.fwpt.config;

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

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
@EnableWebMvc
@EnableSwagger2
public class SpringfoxConfig {

    @Bean
    public Docket petApi() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
                .apis(RequestHandlerSelectors.basePackage("com.founder.fwpt.controller")).build();

    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("服务平台 API").description("").termsOfServiceUrl("http://localhost:8080").version("1.0").build();
    }
}

其中有个信息

  basePackage

指定controller的包。

在spring-mvc的拦截其中增加静态资源访问控制。

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

启动项目,在浏览器中访问

http://localhost:8080/swagger-ui.html

即可。

具体的controller中的@Api注解信息,参考

https://github.com/wkennedy/swagger4spring-web

posted @ 2017-07-14 12:16  mahuan2  阅读(9628)  评论(0编辑  收藏  举报