https://www.jianshu.com/p/ab10860945c3

验证通过

关于Swagger的简介就不占篇幅了...
本文使用的Springfox-Swagger版本为2.8.0

要整合Springfox-Swagger,只需要在Maven导入两个包即可,没有Maven下载导入也行...

        <dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>

选择版本时,最好保持两个包的版本一致,以免出现不可预知的问题~
以上两个是使用Swagger的基本包,如果需要接口自动完成对象和JSON串的转换的话,需要再导入Jackson支持

        <dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.6.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.6.5</version>
</dependency>

导入包以后,创建一个简单的Swagger配置类

package net.sealin.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
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; // 仅在没有Spring-boot的项目中需要开启此配置
@EnableWebMvc
// 启用Swagger2
@EnableSwagger2
// 让Spring来加载该类配置
@Configuration
/**
* 也可在Spring配置文件中配置
* <context:component-scan base-package="net.sealin.controller"/>
*/
@ComponentScan(basePackages = "net.sealin.controller")
/**
* @author Sealin
* Created by Sealin on 2018-03-28.
*/
public class SwaggerConfig {
@Bean
public Docket buildDocket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(buildApiInf())
.select()
//controller匹配规则
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build();
} private ApiInfo buildApiInf() {
return new ApiInfoBuilder()
.title("开放接口API")
.termsOfServiceUrl("http://localhost:8099/v2/api-docs")
.description("项目名称等描述性词语")
.contact(new Contact("Sealin", "http://www.sealin.net/", "admin@sealin.net"))
.version("1.0")
.build(); }
}

要让此配置类生效,需要Spring上下文配置中存在如下选项:

    <!-- 让Swagger可以访问Controller -->
<mvc:annotation-driven />
<!-- 开启注解管理 -->
<context:annotation-config />
<!-- 将我们建立的配置类加入Spring容器 -->
<bean class="net.sealin.config.SwaggerConfig" />

    <!-- 官方说明 -->
<!-- Required so springfox can access spring's RequestMappingHandlerMapping -->
<mvc:annotation-driven/> <!-- Required to enable Spring post processing on @Configuration classes. -->
<context:annotation-config/> <bean class="com.yourapp.configuration.MySwaggerConfig"/>

此外,因为我们用Spring实现的Servlet取代了默认的,在处理Swagger-UI的静态资源时,Spring-Servlet并不会帮我们映射这些资源文件,会导致不能访问swagger-ui.html的情况,两种方式可以解决这个问题,任选一种即可:
一.将没有@Controller解析的请求交给默认Servlet处理

    <mvc:default-servlet-handler />

二.给Spring-servlet指定我们需要映射的资源文件路径

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

至此,Spring和Swagger的整合过程就告一段落了,运行试试:

API文档视图及操作界面:
http://127.0.0.1:8080/swagger/swagger-ui.html
所有API的汇总信息(JSON)
http://127.0.0.1:8080/swagger/v2/api-docs

遇到其他问题可以留言噢,祝君好运~

作者:Sealin
链接:https://www.jianshu.com/p/ab10860945c3
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

SpringMVC整合Springfox-Swagger的更多相关文章

  1. SpringMVC整合SpringFox实践总结

    项目中使用的swagger框架在生成api文档时存在一些问题: 1. 控制器下方法无法点击展开 2.api内容结构混乱 基于上述原因,重新整合重构了一下api文档生成的代码.在此将重整过程记录下来,方 ...

  2. 一步步完成Maven+SpringMVC+SpringFox+Swagger整合示例

    本文给出一个整合Maven+SpringMVC+SpringFOX+Swagger的示例,并且一步步给出完成步骤. 本人在做实例时发现 http://blog.csdn.net/zth1002/art ...

  3. SpringMVC、SpringFox和Swagger整合项目实例

    目标 在做项目的时候,有时候需要提供其它平台(如业务平台)相关的HTTP接口,业务平台则通过开放的HTTP接口获取相关的内容,并完成自身业务~ 提供对外开放HTTP API接口,比较常用的是采用Spr ...

  4. MP实战系列(十)之SpringMVC集成SpringFox+Swagger2

    该示例基于之前的实战系列,如果公司框架是使用JDK7以上及其Spring+MyBatis+SpringMVC/Spring+MyBatis Plus+SpringMVC可直接参考该实例. 不过建议最好 ...

  5. Maven + SpringMVC项目集成Swagger

    Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体目标是使客户端和文件系统作为服务器以同样的速度来更新.文件的方法,参数和模型紧密集成到服 ...

  6. (转)Dubbo与Zookeeper、SpringMVC整合和使用

    原文地址: https://my.oschina.net/zhengweishan/blog/693163 Dubbo与Zookeeper.SpringMVC整合和使用 osc码云托管地址:http: ...

  7. SSM整合(三):Spring4与Mybatis3与SpringMVC整合

    源码下载 SSMDemo 上一节整合了Mybatis3与Spring4,接下来整合SpringMVC! 说明:整合SpringMVC必须是在web项目中,所以前期,新建的就是web项目! 本节全部采用 ...

  8. Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)

    互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ...

  9. springmvc整合fastjson

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  10. 【转】Dubbo_与Zookeeper、SpringMVC整合和使用(负载均衡、容错)

    原文链接:http://blog.csdn.net/congcong68/article/details/41113239 互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服 ...

随机推荐

  1. SURF算法源代码OPENSURF分析

    SURF算法源代码分析 平台:win x64 + VS2015专业版 +opencv2.4.11 配置类似参考OPENSIFT,参考我的另一篇博客:https://www.cnblogs.com/Al ...

  2. Oralce问题之ORA-12560:TNS协议适配器错误

    在Windows系统中,通过CMD命令打开命令窗口,通过命令:sqlplus / as sysdba回车后提示 协议适配器错误 可能原因: (1).Oralce数据库监听服务没启动起来 通过开始-&g ...

  3. java 中的 Math.round(-1.5) 等于多少?(未完成)

    java 中的 Math.round(-1.5) 等于多少?(未完成)

  4. javascript 常用的一些原生方法

    一丶javascript------ reduce() reduce()方法: arr.reduce(function(prev,cur,index,arr){ ... }, init); 参数解释: ...

  5. glRenderbufferStorageMultisample

    https://www.khronos.org/registry/OpenGL-Refpages/es3.0/html/glRenderbufferStorage.xhtml https://www. ...

  6. C语言Ⅰ作业-05

    这个作业属于哪个课程 C语言程序设计Ⅰ 这个作业要求在哪里 https://www.cnblogs.com/tongyingjun/p/11722665.html 我在这个课程的目标是 熟练掌握如何用 ...

  7. TypeError: Cannot read property '$$' of undefined at HTMLElement._attached.wx.getPlatform._touchstartHandlerForDevtools

    TypeError: Cannot read property '$$' of undefined     at HTMLElement._attached.wx.getPlatform._touch ...

  8. Java web中不同浏览器间导出Excel文件名称乱码问题解决方案

    问题描述: 对于不同浏览器存在对中文编码格式问题,从而在导出Excel文件时,中文文件名出现乱码的情况,即在程序中给要导出的文件指定一个中文名字时,在浏览器上出现的下载框中的文件名出现了乱码,解决如下 ...

  9. centos7编译安装PHP7已经把你逼到去安定医院看门诊的地步?请看此文

    本文援引自https://www.cnblogs.com/lamp01/p/10101659.html,亲测可行,特此鸣谢 地球上总有一群人是如此深爱PHP,但无奈的是编译安装的过程化特性,导致各种b ...

  10. Waiting (TTFB) 时间

    什么是 Waiting (TTFB) 时间 TTFB 是 Time to First Byte 的缩写,指的是浏览器开始收到服务器响应数据的时间(后台处理时间+重定向时间),是反映服务端响应速度的重要 ...