一、swagger2依赖

<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<exclusions>
<exclusion>
<artifactId>spring-aop</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
<exclusion>
<artifactId>jackson-annotations</artifactId>
<groupId>com.fasterxml.jackson.core</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
</dependency>

二、springmvc配置文件加入

<mvc:default-servlet-handler />

三、web.xml配置

<welcome-file-list>
<welcome-file>swagger-ui.html</welcome-file>
</welcome-file-list>

四、swagger2配置

  可创建多个Docket,对restful api进行分组管理

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
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; @EnableWebMvc
@EnableSwagger2
@Configuration
public class Swagger2Config extends WebMvcConfigurationSupport { @Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("admin")
.select()
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.ant("/**"))
.build();
} // @Bean
// public Docket xxx() {
// return new Docket(DocumentationType.SWAGGER_2)
// .apiInfo(apiInfo())
// .groupName("xxx")
// .select()
// .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
// .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
// .paths(PathSelectors.ant("/xxx/**"))
// .build();
// } private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("shiro 无状态组件")
.contact(new Contact("胡俊哲个", "", "2570230521@qq.com"))
.version("1.0")
.build();
}
}

五、效果演示

Restful API 访问路径:
* http://IP:port/{context-path}/swagger-ui.html

六、注意事项

  1、如果有拦截器或者过滤器 对项目根路径进行拦截,可能<welcome-file>的配置不生效!

  2、如果 配置 <welcome-file>swagger-ui.html</welcome-file>,访问 http://IP:port/{context-path}/,虽然可以看到页面,但是接口内容显示不出来。原因如下:

  

  这个js在匹配url的时候 要包含“swagger-ui.html”,所以就出错了。解决方案如下:

  (1)修改web.xml

 <welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>

  (2)新建index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<script type="application/javascript">
window.location.href = window.location.origin + "/shiro/swagger-ui.html";
</script>
</body>
</html>

七、swagger2常用注解参考

  Swagger-Core Annotations

springmvc+swagger2的更多相关文章

  1. SpringMVC集成springfox-swagger2自动生成接口文档

    本节内容: 什么是Swaggger Springfox与Swagger的关系 SpringMVC集成springfox-swagger2 一.什么是Swaggger Swagger是一个流行的API开 ...

  2. springmvc集成swaggerui

    这里先写下需要的pom.xml配置(我引用的2.4.0,相对稳定) 在集成springfox-swagger2之前,我也尝试着集成了swagger-springmvc,方式差不多,但是swagger- ...

  3. SpringMVC中使用Swagger2整合

    Swagger2是什么 Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件. Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 W ...

  4. 快速构建springmvc+spring+swagger2环境

    快速构建springmvc+spring+swagger2环境 开发工具:Intellij idea               jdk: 1.8 开发步骤: 1.创建maven工程,如图建立工程结构 ...

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

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

  6. 03、Swagger2和Springmvc整合详细记录(爬坑记录)

    时间 内容 备注 2018年6月18日 基本使用 spirngmvc整合swagger2 开始之前这个系列博文基本是,在项目的使用中一些模块的内容记录,但是后期逐渐优化,不单单是整合内容. swagg ...

  7. springmvc与swagger2

    首先呢我们导入相关的jar包文件 为了方便copy我copy一份 <!-- 导入java ee jar 包 -->        <dependency>           ...

  8. 使用Swagger2构建SpringMVC项目中的Restful API文档

    使用Swagger自动生成API文档,不仅增加了项目的可维护性,还提高了API的透明度更利于快速测试等工作,便于更快地发现和解决问题. 本篇文章只记录整合过程,关于Security Configura ...

  9. 基于Maven的Springboot+Mybatis+Druid+Swagger2+mybatis-generator框架环境搭建

    基于Maven的Springboot+Mybatis+Druid+Swagger2+mybatis-generator框架环境搭建 前言 最近做回后台开发,重新抓起以前学过的SSM(Spring+Sp ...

随机推荐

  1. BZOJ 1031 [JSOI2007]字符加密Cipher 后缀数组教程

    1031: [JSOI2007]字符加密Cipher Description 喜欢钻研问题的JS同学,最近又迷上了对加密方法的思考.一天,他突然想出了一种他认为是终极的加密办法:把需要加密的信息排成一 ...

  2. ASP.NET MVC验证框架中关于属性标记的通用扩展方法

    http://www.cnblogs.com/wlb/archive/2009/12/01/1614209.html 之前写过一篇文章<ASP.NET MVC中的验证>,唯一的遗憾就是在使 ...

  3. C&C++图形图像处理开源库

    Google三维APIO3D O3D 是一个开源的 WebAPI 用来在浏览器上创建界面丰富的交互式的 3D 应用程序.这是一种基于网页的可控3D标准.此格式期望真正的基于浏览器,独立于操作系统之外, ...

  4. phpstorm 配置 webpack @ 别名跳转

    webstorm中专门有webpack的相关配置,默认的路径直接是项目根目录下的 webpack.config.js,但是我们用各种cli生成的项目中,webpack的配置一般都是在build下,导致 ...

  5. win32 ini

    原文:https://www.cnblogs.com/qq78292959/archive/2012/06/10/2544389.html Windows操作系统专门为此提供了6个API函数来对配置设 ...

  6. C语言复习---选择法排序

    选择排序也是一种简单直观的排序算法 它的工作原理很容易理解:初始时在序列中找到最小(大)元素,放到序列的起始位置作为已排序序列:然后,再从剩余未排序元素中继续寻找最小(大)元素,放到已排序序列的末尾. ...

  7. POJ - 3026 Borg Maze(最小生成树)

    https://vjudge.net/problem/POJ-3026 题意 在一个y行 x列的迷宫中,有可行走的通路空格’ ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用最短的 ...

  8. CF&&CC百套计划4 Codeforces Round #276 (Div. 1) A. Bits

    http://codeforces.com/contest/484/problem/A 题意: 询问[a,b]中二进制位1最多且最小的数 贪心,假设开始每一位都是1 从高位i开始枚举, 如果当前数&g ...

  9. 流媒体技术学习笔记之(八)海康、大华IpCamera RTSP地址和格式

    海康: rtsp://[username]:[password]@[ip]:[port]/[codec]/[channel]/[subtype]/av_stream 说明: username: 用户名 ...

  10. windows环境命令行创建虚拟环境

    1:安装virtualenv pip install virtualenv 2:创建并激活虚拟环境 #创建虚拟环境 D:\>mkdir xianmu D:\>cd xianmu D:\xi ...