整合spring mvc和swagger(Maven作为项目管理工具)

  一、Swagger是什么?

The OpenAPI specification (formerly known as the Swagger Specification) is a powerful definition format to describe RESTful APIs. The specification creates a RESTful interface for easily developing and consuming an API by effectively mapping all the resources and operations associated with it. It’s easy-to-learn, language agnostic, and both human and machine readable.

OpenAPI规范(以前称为Swagger规范)是描述RESTful API的强大的定义格式。该规范创建了一个RESTful接口,用于轻松开发和使用API,有效地映射与其相关联的所有资源和操作。它是易于学习,语言不可知,人性化和机器可读性。

个人目前理解:swagger是易于测试,易上手,简单明了的在线openapi文档。可以不用写接口文档了,项目中的接口一目了然。能够根据swagger提供的注释来解释每个接口以及每个接口参数的含义。也可以不用拿postman进行手工测试,能够直接进行测试。

二、spring mvc 整合Swagger

  springmvc版本: 4.2.6 Release

  swagger-spring版本:2.6.1

  1、pom.xml增加依赖

  

<!-- swagger -->
    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger2</artifactId>
      <version>${springfox.swagger.version}</version>
    </dependency>
    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger-ui</artifactId>
      <version>${springfox.swagger.version}</version>
    </dependency>

   2、增加swagger在spring的配置类

  

package com.hua.tpwg.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.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * Swagger配置
 *
 */
@EnableWebMvc
@EnableSwagger2
@Configuration
public class SwaggerConfig {

  @Bean
  public Docket createRestApi() {
    return new Docket(DocumentationType.SWAGGER_2)
        .apiInfo(apiInfo())
        .select()
        .apis(RequestHandlerSelectors.basePackage("com.hua.tpwg.controller")) 、
        .paths(PathSelectors.any())
        .build();
  }

  private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
        .title("接口列表 v1.1.0") //接口文档的title
        .description("接口测试") // 接口文档的描述
        .termsOfServiceUrl("http://localhost:8090/web/swagger-ui.html") 
        .contact(new Contact("laohu", "http://localhost:8090/web", "111@qq.com"))//联系信息 
        .version("1.1.0")
        .build();
  }
}

  3、spring-mvc.xml或者其他springmvc的配置xml中增加以下配置

  

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

  4、启动项目访问

  

  三、注意事项

  1、如果项目中有shiro,需要把swagger需要请求链接放开,不然会有验证。(不知道是否需要加验证)

  
/swagger*/**=anon
/v2/**=anon
/webjars/**=anon

 

  2、链接需要加项目的contentroot,比如

  注:此文借鉴了网友的一篇博文 http://blog.csdn.net/blackmambaprogrammer/article/details/72354007

posted @ 2017-10-13 16:45  西恩yang  阅读(251)  评论(0编辑  收藏  举报