一、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. 【CF61D】Eternal Victory

    题目大意:给定一棵 N 个节点的树,求从 1 号节点(根节点)出发,任意节点结束,且至少经过每个节点一次的最短路径是多少. 题解:首先考虑最终要回到根节点的情况,可以发现最短路径长度一定等于该树边权的 ...

  2. SQL Server 行列相互转换命令:PIVOT和UNPIVOT使用详解

    一.使用PIVOT和UNPIVOT命令的SQL Server版本要求 1.数据库的最低版本要求为SQL Server 2005 或更高. 2.必须将数据库的兼容级别设置为90 或更高. 3.查看我的数 ...

  3. SQL Server 事务与隔离级别实例讲解

    上班途中,你在一处ATM机前停了下来.正当你在敲入密码的时候,你的一位家人也正在镇上的另一处TAM机上输入密码.你打算从某个还有500元余额的账户上转出400元,而你的家人想从同一账户取走300元.倘 ...

  4. Flask-SQLAlchemy 无法创建Sqlite 数据库???

    <Flask web 开发>第五章数据库照书中的方法无法创建表,没有python的命令提示符,只是运行程序输入db.create_all()后文件夹里也没有data.sqlite文件 解决 ...

  5. linux查看硬盘详细信息

    最近公司新进了一批设备,为了弄清楚厂家有没有忽悠我们,分配给我一个任务 查看磁盘是不是二手的,window上有鲁大师啥的那么linux有什么软件吗? 答案是有的! 叫做smart 具体请查看: htt ...

  6. 高级篇 KZ002.反射读取注解[未封装]

    创建自定义注解 package com.hanpang.java; /** * 注解说明: 方法的文档注释 * * @Author: 胖先生 * @Create: 2016-04-27 10:29 * ...

  7. vue中的this指向问题

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. Spring Cloud(十四)Config 配置中心与客户端的使用与详细

    前言 在上一篇 文章 中我们直接用了本应在本文中配置的Config Server,对Config也有了一个基本的认识,即 Spring Cloud Config 是一种用来动态获取Git.SVN.本地 ...

  9. 深入分析Java Web技术内幕

    深入web请求过程 发起一个http请求的过程就是建立一个socket通信的过程 HTTPClient是一个开源的实现了http请求的工具包 深入分析java I/O的工作机制 深入分析java We ...

  10. javascript多种继承方式(函数式,浅复制,深复制,函数绑定和借用)

    函数式继承: var object = function (obj) { if (typeof Object.create !== 'undefined') { return Object.creat ...