一、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 {@Beanpublic 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

转载于:https://www.cnblogs.com/hujunzheng/p/7210140.html

springmvc+swagger2相关推荐

  1. springMVC swagger2

    参考地址:https://www.cnblogs.com/exmyth/p/7183753.html https://blog.csdn.net/programmer_sean/article/det ...

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

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

  3. SpringMVC整合Swagger2

    SpringMVC整合Swagger2 前言 POM引入 Swagger配置类 spring-mvc.xml设置 如果有在web.xml里配置拦截器 测试类 准备就绪 注意事项 爬坑经历 参考文章 前 ...

  4. dubbo2.5-spring4-mybastis3.2-springmvc4-mongodb3.4-redis3(十)之Spring MVC中使用 Swagger2 构建Restful API...

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u010046908/article/details/55047193 1.Swagger2是什么? ...

  5. 基于Spring+SpringMVC+Mybatis的分布式敏捷开发系统架构(附源码)

    点击上方 好好学java ,选择 星标 公众号重磅资讯,干货,第一时间送达 今日推荐:推荐19个github超牛逼项目!个人原创100W +访问量博客:点击前往,查看更多 作者:zheng gitee ...

  6. SpringBoot 使用Swagger2打造在线接口文档(附源代码)

    点击上方"好好学java",选择"置顶公众号" 优秀学习资源.干货第一时间送达! 精彩内容 java实战练习项目教程 2018微服务资源springboot.s ...

  7. SSM三大框架整合Springfox(Swagger2)详细解决方案

    由于项目中使用的是前后端分离,所以,频繁的需要进行数据的交互和接口的调用,所以需要api文档的使用,这样就更加的方便,于是就找到了swagger这个东东,还是很好用.下面介绍一下如何整合到spring ...

  8. Swagger UI 与SpringMVC的整合 II

    pom.xml <!-- swagger开始 --><dependency><groupId>io.springfox</groupId><art ...

  9. SpringMVC+JWT+Swagger UI+RestFul

    前言: 其实很早就想写这篇文章了,因为我觉得这会对很多新手有指引作用,当初自己也是瞎子过河的摸索着过来的.目前后台开发比较流行的MVC框架中使用Spring MVC还是比较多的,当然还有Spring ...

最新文章

  1. 7 centos 时钟跟物理机同步_通信网中的同步技术
  2. 折纸机器人的步骤图解_折纸图解走路的人
  3. Two Merged Sequences(CF 1144 G)
  4. js中的Object.create(null) 和 {} 的区别
  5. (4) XOS 源码详解: os_s_xxxx.s 汇编代码的 堆栈空间定义,比较简洁的方式
  6. Kali Linux Network Scanning Cookbook读书笔记之nmap
  7. 第四章 ASP.NET MVC HTML辅助方法生成表单标签具体用法
  8. php html转ubb,php实现转换ubb代码的方法
  9. 使用TF卡烧录Jetson NX开发板
  10. linux系统共享文件夹,Linux系统如何设置共享文件夹?新建文件夹以及指令是什么?...
  11. 即时聊天软件开发体会
  12. 粗虚线和细虚线_高速虚线两侧是粗虚线 高速公路虚线两边加斜线什么意思?...
  13. 学习Nginx这一篇就够了(非本人原创文章)
  14. CTB全球创新大挑战组队中 全新课题火热来袭
  15. VisionPro 报错解决方法
  16. c语言习题---(if语句)
  17. 计算机运行快捷方式,电脑运行快捷键有哪些 电脑运行快捷键介绍
  18. 注册Netgear DDNS账号的操作
  19. h3c如何配置acl命令
  20. PMP到底是啥,你竟然还不知道?

热门文章

  1. vos3000落地网关对接教学_跨国合作:Serverless Components 在腾讯云的落地和实践
  2. (进阶篇)Redis6.2.0 集群 哨兵模式_故障转移_03
  3. Centos7 1秒钟 安装 Docker
  4. Linux通过RPM方式指定软件安装目录
  5. RabbitMQ 最新版本 下载、部署 _rpm版本(CentOS7环境)
  6. linux CentOS7 erlang安装
  7. Linux Shell脚本专栏_一键查看服务器的利用率_04
  8. 工作流实战_09_flowable 流程实例挂起与激活
  9. Swagger的描述注释配置详解
  10. Mybatisplus代码生成 之SpringBoot适配MYSQL和ORACLE