Swagger使用

1. Spring MVC配置文件中的配置

<mvc:annotation-driven/>

<context:component-scan base-package="com.demo"/>

设置使用注解的类所在的jar包,只加载controller类

<mvc:default-servlet-handler />

使用 Swagger Restful API文档时,添加此注解

2. maven依赖

<properties>

<spring.version>4.2.6.RELEASE</spring.version>

<servlet.version>3.1.0</servlet.version>

<swagger2.version>2.5.0</swagger2.version>

</properties>

<dependencies>

<!-- swagger开始  -->

<dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox-swagger2</artifactId>

<version>${swagger2.version}</version>

</dependency>

<dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox-swagger-ui</artifactId>

<version>${swagger2.version}</version>

</dependency>

<dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox-staticdocs</artifactId>

<version>${swagger2.version}</version>

</dependency>

<!--  swagger结束  -->

<!--spring-->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-core</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-web</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-webmvc</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-beans</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context-support</artifactId>

<version>${spring.version}</version>

</dependency>

<!--web-->

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>javax.servlet-api</artifactId>

<version>${servlet.version}</version>

<scope>provided</scope>

</dependency>

</dependencies>

3. Swagger注解配置

@RequestMapping(value = "/add", method = RequestMethod.POST)

@ApiOperation(value = "添加用户", notes = "增加用户")

public Result<UserVo> add(@ApiParam(name = "userName",value = "用户昵称",required = true)@RequestParam(name = "userName",required = true)String userName,

@ApiParam(name = "mobile",value = "手机",required = true)@RequestParam(name = "mobile",required = true)String mobile,

@ApiParam(required = true, name = "email", value = "邮箱") @RequestParam(name = "email", required = true) String email ) {

UserVo userVo=new UserVo();

userVo.setUserId(System.currentTimeMillis());

userVo.setUserName(userName);

return new Result<UserVo>(Constants.SUCCESS,Constants.MSG_SUCCESS,userVo);

}

@ApiOperation(value = "创建用户", notes = "根据UserVo对象创建用户")

@RequestMapping(value = "/create", method = RequestMethod.POST)

public String postUser(@ApiParam(required = true, name = "userVo", value = "实体UserVo") @RequestBody UserVo userVo) {

return "success";

}

@RequestMapping(value = "/getUser", method = RequestMethod.GET)

@ApiOperation(value = "获取用户", notes = "根据UserVo获取用户")

@ResponseBody

public Result<UserVo> getUser() {

UserVo userVo=new UserVo();

userVo.setUserId(System.currentTimeMillis());

userVo.setUserName("UserName");

return new Result<UserVo>(Constants.SUCCESS,Constants.MSG_SUCCESS,userVo);

}

SwaggerConfig类编写示例

@Configuration

@EnableSwagger2

public class SwaggerConfig {

@Bean

public Docket userApi() {

return new Docket(DocumentationType.SWAGGER_2)

.groupName("用户")

.select()  // 选择那些路径和api会生成document

.apis(RequestHandlerSelectors.basePackage("com.zyx.controller"))

.paths(PathSelectors.any()) // 对所有路径进行监控

.build()

.apiInfo(userInfo());

}

private ApiInfo userInfo() {

ApiInfo apiInfo = new ApiInfo("用户相关接口",//大标题

"用户有关的接口,包括增加删除用户",//小标题

"0.1",//版本

"杭州",

new Contact("zyx", "", ""),// 作者

"swagger url",//链接显示文字

""//网站链接

);

return apiInfo;

}

@Bean

public Docket otherApi() {

return new Docket(DocumentationType.SWAGGER_2)

.groupName("其它")

.select()  // 选择那些路径和api会生成document

.apis(RequestHandlerSelectors.basePackage("com.zyx.controller"))

.paths(PathSelectors.any()) // 对所有路径进行监控

.build()

.apiInfo(otherInfo());

}

private ApiInfo otherInfo() {

ApiInfo apiInfo = new ApiInfo("其它相关接口",//大标题

"其它有关的接口,包括增加删除其它",//小标题

"0.1",//版本

"杭州",

new Contact("zyx_other", "", ""),// 作者

"点击",//链接显示文字

""//网站链接

);

return apiInfo;

}

}

注:将SwaggerConfig在Spring注入管理 <bean class="com.demo.config.SwaggerConfig"/>

@ApiModel(value = "用户信息")

public class UserVo {

@ApiModelProperty(value = "用户id", required = true)

private long userId;

@ApiModelProperty(value = "昵称", required = true)

private String userName;

public long getUserId() {

return userId;

}

public void setUserId(long userId) {

this.userId = userId;

}

public String getUserName() {

return userName;

}

public void setUserName(String userName) {

this.userName = userName;

}

}

4. 界面展示

在浏览器输入地址:http://localhost:8080/swagger-ui.html

【原创】Sagger使用的更多相关文章

  1. 【原创分享·支付宝支付】HBuilder打包APP调用支付宝客户端支付

    前言 最近有点空余时间,所以,就研究了一下APP支付.前面很早就搞完APP的微信支付了,但是由于时间上和应用上的情况,支付宝一直没空去研究.然后等我空了的时候,发现支付宝居然升级了支付逻辑,虽然目前还 ...

  2. 【原创分享·微信支付】C# MVC 微信支付教程系列之现金红包

            微信支付教程系列之现金红包           最近最弄这个微信支付的功能,然后扫码.公众号支付,这些都做了,闲着无聊,就看了看微信支付的其他功能,发现还有一个叫“现金红包”的玩意,想 ...

  3. 【原创分享·微信支付】 C# MVC 微信支付教程系列之扫码支付

    微信支付教程系列之扫码支付                  今天,我们来一起探讨一下这个微信扫码支付.何为扫码支付呢?这里面,扫的码就是二维码了,就是我们经常扫一扫的那种二维码图片,例如,我们自己添 ...

  4. 【原创分享·微信支付】 C# MVC 微信支付教程系列之公众号支付

    微信支付教程系列之公众号支付         今天,我们接着讲微信支付的系列教程,前面,我们讲了这个微信红包和扫码支付.现在,我们讲讲这个公众号支付.公众号支付的应用环境常见的用户通过公众号,然后再通 ...

  5. 【原创分享·微信支付】C# MVC 微信支付之微信模板消息推送

    微信支付之微信模板消息推送                    今天我要跟大家分享的是“模板消息”的推送,这玩意呢,你说用途嘛,那还是真真的牛逼呐.原因在哪?就是因为它是依赖微信生存的呀,所以他能不 ...

  6. [原创]java使用JDBC向MySQL数据库批次插入10W条数据测试效率

    使用JDBC连接MySQL数据库进行数据插入的时候,特别是大批量数据连续插入(100000),如何提高效率呢?在JDBC编程接口中Statement 有两个方法特别值得注意:通过使用addBatch( ...

  7. GJM : C#设计模式汇总整理——导航 【原创】

    感谢您的阅读.喜欢的.有用的就请大哥大嫂们高抬贵手"推荐一下"吧!你的精神支持是博主强大的写作动力以及转载收藏动力.欢迎转载! 版权声明:本文原创发表于 [请点击连接前往] ,未经 ...

  8. 信息安全-5:RSA算法详解(已编程实现)[原创]

    转发注明出处:http://www.cnblogs.com/0zcl/p/6120389.html 背景介绍 1976年以前,所有的加密方法都是同一种模式: (1)甲方选择某一种加密规则,对信息进行加 ...

  9. Atitit.你这些项目不都是模板吗?不是原创  集成和整合的方式大总结

    Atitit.你这些项目不都是模板吗?不是原创  集成和整合的方式大总结 1.1. 乔布斯的名言:创新即整合(Creativity is just connecting things).1 1.2. ...

随机推荐

  1. C++创建一个名为Ellipse的椭圆类--练习

    题目描述: /*设计名为Ellipse的椭圆类*/ /* 其属性为外接矩形的左上角与右下角两个点的坐标,并能计算出椭圆的面积,并测试该类. */ 代码如下: #include<iostream& ...

  2. js文件处理File

    支持File API的浏览器有IE10+,Firefox3.5+,Opera10.6+,Safari5+,Chrome. 1.在表单元素上<input type="fiel" ...

  3. Phpstrom开发工具

    下载地址 https://www.jetbrains.com/zh/phpstorm/specials/phpstorm/phpstorm.html?utm_source=baidu&utm_ ...

  4. 日志管理——rsyslog

    官方文档(必看) http://www.rsyslog.com/doc/v8-stable/ 简介 rsyslog是linux自带日志管理工具,分为客户端\服务端,包含日志收集\过滤\分析\转储. 数 ...

  5. 基于OMAPL138的Linux字符驱动_GPIO驱动AD9833(二)之cdev与read、write

    基于OMAPL138的Linux字符驱动_GPIO驱动AD9833(二)之cdev与read.write 0. 导语 在上一篇博客里面,基于OMAPL138的字符驱动_GPIO驱动AD9833(一)之 ...

  6. PublicCMS 网站漏洞 任意文件写入并可提权服务器权限

    PublicCMS是目前网站系统中第一个采用JAVA架构 TOMCAT+Apcche+Mysql数据库架构的CMS网站,开源,数据承载量大,可以承载到上千万的数据量,以及用户的网站并发可达到上千万的P ...

  7. powerpoint教程资料,PPT的

    Powerpoint,是微软公司设计的演示文稿软件,利用Powerpoint不仅可以创建演示文稿,还可以在互联网上召开面对面会议.远程会议或在网上给观众展示演示文稿,掌握利用PowerPoint是一项 ...

  8. 预防跨站脚本(xss)

    对xss的防护方法结合在两点上输入和输出,一是严格控制用户表单的输入,验证所有输入数据,有效监测到攻击,go web表单中涉及到.二是对所有输出的数据进行处理,防止已成功注入的脚本在浏览器端运行. 在 ...

  9. ABAP CDS ON HANA-(5)テーブル結合ビュー

    JOINs in CDS View In ABAP CDS, Join between two data sources is allowed. Allowed joins are:- Inner J ...

  10. gp的纯属意外的意外

    一不小心,把方法都传过去了,一脸蒙蔽说的就是我,啊哈哈哈啊哈