springboot + swagger
swagger用于定义API文档。
好处:
- 前后端分离开发
- API文档非常明确
- 测试的时候不需要再使用URL输入浏览器的方式来访问Controller
- 传统的输入URL的测试方式对于post请求的传参比较麻烦(当然,可以使用postman这样的浏览器插件)
- spring-boot与swagger的集成简单的一逼
1、项目结构
和上一节一样,没有改变。
2、pom.xml
引入了两个jar。

1 <dependency>
2 <groupId>io.springfox</groupId>
3 <artifactId>springfox-swagger2</artifactId>
4 <version>2.2.2</version>
5 </dependency>
6 <dependency>
7 <groupId>io.springfox</groupId>
8 <artifactId>springfox-swagger-ui</artifactId>
9 <version>2.2.2</version>
10 </dependency>

3、Application.java

1 package com.xxx.firstboot;
2
3 import org.springframework.boot.SpringApplication;
4 import org.springframework.boot.autoconfigure.SpringBootApplication;
5
6 import springfox.documentation.swagger2.annotations.EnableSwagger2;
7
8 @SpringBootApplication //same as @Configuration+@EnableAutoConfiguration+@ComponentScan
9 @EnableSwagger2 //启动swagger注解
10 public class Application {
11
12 public static void main(String[] args) {
13 SpringApplication.run(Application.class, args);
14 }
15
16 }

说明:
- 引入了一个注解@EnableSwagger2来启动swagger注解。(启动该注解使得用在controller中的swagger注解生效,覆盖的范围由@ComponentScan的配置来指定,这里默认指定为根路径"com.xxx.firstboot"下的所有controller)
4、UserController.java

1 package com.xxx.firstboot.web;
2
3 import org.springframework.beans.factory.annotation.Autowired;
4 import org.springframework.web.bind.annotation.RequestHeader;
5 import org.springframework.web.bind.annotation.RequestMapping;
6 import org.springframework.web.bind.annotation.RequestMethod;
7 import org.springframework.web.bind.annotation.RequestParam;
8 import org.springframework.web.bind.annotation.RestController;
9
10 import com.xxx.firstboot.domain.User;
11 import com.xxx.firstboot.service.UserService;
12
13 import io.swagger.annotations.Api;
14 import io.swagger.annotations.ApiImplicitParam;
15 import io.swagger.annotations.ApiImplicitParams;
16 import io.swagger.annotations.ApiOperation;
17 import io.swagger.annotations.ApiResponse;
18 import io.swagger.annotations.ApiResponses;
19
20 @RestController
21 @RequestMapping("/user")
22 @Api("userController相关api")
23 public class UserController {
24
25 @Autowired
26 private UserService userService;
27
28 // @Autowired
29 // private MyRedisTemplate myRedisTemplate;
30
31 @ApiOperation("获取用户信息")
32 @ApiImplicitParams({
33 @ApiImplicitParam(paramType="header",name="username",dataType="String",required=true,value="用户的姓名",defaultValue="zhaojigang"),
34 @ApiImplicitParam(paramType="query",name="password",dataType="String",required=true,value="用户的密码",defaultValue="wangna")
35 })
36 @ApiResponses({
37 @ApiResponse(code=400,message="请求参数没填好"),
38 @ApiResponse(code=404,message="请求路径没有或页面跳转路径不对")
39 })
40 @RequestMapping(value="/getUser",method=RequestMethod.GET)
41 public User getUser(@RequestHeader("username") String username, @RequestParam("password") String password) {
42 return userService.getUser(username,password);
43 }
44
45 // @RequestMapping("/testJedisCluster")
46 // public User testJedisCluster(@RequestParam("username") String username){
47 // String value = myRedisTemplate.get(MyConstants.USER_FORWARD_CACHE_PREFIX, username);
48 // if(StringUtils.isBlank(value)){
49 // myRedisTemplate.set(MyConstants.USER_FORWARD_CACHE_PREFIX, username, JSON.toJSONString(getUser()));
50 // return null;
51 // }
52 // return JSON.parseObject(value, User.class);
53 // }
54
55 }

说明:
- @Api:用在类上,说明该类的作用
- @ApiOperation:用在方法上,说明方法的作用
- @ApiImplicitParams:用在方法上包含一组参数说明
- @ApiImplicitParam:用在@ApiImplicitParams注解中,指定一个请求参数的各个方面
- paramType:参数放在哪个地方
- header-->请求参数的获取:@RequestHeader
- query-->请求参数的获取:@RequestParam
- path(用于restful接口)-->请求参数的获取:@PathVariable
- body(不常用)
- form(不常用)
- name:参数名
- dataType:参数类型
- required:参数是否必须传
- value:参数的意思
- defaultValue:参数的默认值
- paramType:参数放在哪个地方
- @ApiResponses:用于表示一组响应
- @ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息
- code:数字,例如400
- message:信息,例如"请求参数没填好"
- response:抛出异常的类
- @ApiModel:描述一个Model的信息(这种一般用在post创建的时候,使用@RequestBody这样的场景,请求参数无法使用@ApiImplicitParam注解进行描述的时候)
- @ApiModelProperty:描述一个model的属性
以上这些就是最常用的几个注解了。
需要注意的是:
- ApiImplicitParam这个注解不只是注解,还会影响运行期的程序,例子如下:
如果ApiImplicitParam中的phone的paramType是query的话,是无法注入到rest路径中的,而且如果是path的话,是不需要配置ApiImplicitParam的,即使配置了,其中的value="手机号"也不会在swagger-ui展示出来。
具体其他的注解,查看:
https://github.com/swagger-api/swagger-core/wiki/Annotations#apimodel
测试:
启动服务,浏览器输入"http://localhost:8080/swagger-ui.html"
最上边一个红框:@Api
GET红框:method=RequestMethod.GET
右边红框:@ApiOperation
parameter红框:@ApiImplicitParams系列注解
response messages红框:@ApiResponses系列注解
输入参数后,点击"try it out!",查看响应内容:
http://www.cnblogs.com/java-zhao/p/5348113.html
springboot + swagger的更多相关文章
- SpringBoot+Swagger整合API
SpringBoot+Swagger整合API Swagger:整合规范的api,有界面的操作,测试 1.在pom.xml加入swagger依赖 <!--整合Swagger2配置类--> ...
- springboot+swagger集成
一.swagger介绍 Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件.本文简单介绍了在项目中集成swagger的方法和一些常见问题.如果想深入分析项目源码,了解更多内容 ...
- springboot+swagger接口文档企业实践(下)
目录 1.引言 2. swagger接口过滤 2.1 按包过滤(package) 2.2 按类注解过滤 2.3 按方法注解过滤 2.4 按分组过滤 2.4.1 定义注解ApiVersion 2.4.2 ...
- springboot+swagger接口文档企业实践(上)
目录 1.引言 2.swagger简介 2.1 swagger 介绍 2.2 springfox.swagger与springboot 3. 使用springboot+swagger构建接口文档 3. ...
- Swagger详解(SpringBoot+Swagger集成)(转)
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/ai_miracle/article/de ...
- 【转】Swagger详解(SpringBoot+Swagger集成)
Swagger-API文档接口引擎Swagger是什么 Swagger是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体目标是使客户端和文件系统作为服务器 ...
- 第四章 springboot + swagger(转载)
此篇博客转发自:http://www.cnblogs.com/java-zhao/p/5348113.html swagger用于定义API文档. 好处: 前后端分离开发 API文档非常明确 测试的时 ...
- 【转载】springboot + swagger
注:本文参考自 http://www.jianshu.com/p/0465a2b837d2 https://www.cnblogs.com/java-zhao/p/5348113.html swagg ...
- 【第四章】 springboot + swagger
注:本文参考自 http://www.jianshu.com/p/0465a2b837d2 swagger用于定义API文档. 好处: 前后端分离开发 API文档非常明确 测试的时候不需要再使用URL ...
随机推荐
- Octopus系列之各个页面调用示例
调用首页产品 可选参数如下 New = 1, Hot = 2, Best = 3, Special = 4, Featured = 5, Other = 6 #foreach($item in $oc ...
- WCF初探-8:WCF服务承载 (上)
前言 任何一个程序的运行都需要依赖一个确定的进程中,WCF服务也不例外.如果使用WCF服务,我们就必须将服务承载于创建它并控制它的上下文和生存期的运行时环境中,承载服务环境的程序,我们称之为宿主.WC ...
- [vijos P1524] 最小监视代价
历时四天(本周三至本周六),本人的第一道网络流题目终于通过了…虽然这么慢才搞懂很大程度是因为脑子笨,但是还是要吐槽一下: (1)选的这道题吧居然是无向图,对于初学者我表示呵呵,昨晚到现在一直在纠结怎么 ...
- enmo_day_10
RMAN 创建备份集 : backup as backupset format ‘/backup/df_%d_%s_%p/bus’ tablespace hr_data; 创建镜像副本 :(备份慢,恢 ...
- mybatis sql in 查询
mybatis官方学习文档:http://www.mybatis.org/core/getting-started.html 本文转自:http://www.blogjava.net/xmatthew ...
- springmvc学习第三天
利用spring mvc 实现crud 1.导入jar包 commons-logging-1.2.jarjstl.jarspring-aop-4.1.6.RELEASE.jarspring-beans ...
- VC中预处理指令与宏定义详解
刚接触到MFC编程的人往往会被MFC 向导生成的各种宏定义和预处理指令所吓倒,但是预处理和宏定义又是C语言的一个强大工具.使用它们可以进行简单的源代码控制,版本控制,预警或者完成一些特殊的功能. 一个 ...
- RedHad中yum安装与使用
yum的安装对于linux来说,是一个福音,至少安装软件来说,非常非常方便,以前使用rpm安装,那个各种依赖,哎,说多了都是泪,现在有这个yum就方便多了. 此处记录redhad的安装.其实我也是借鉴 ...
- USACO 08-Nov( 最小生成树)
美国人出题拐弯抹角,倒是挺尊重动物的 10206301 2 52 3 52 4 123 4 172 5 153 5 64 5 12 Hint从牧场4起床, 然后按照 4, 5, 4, 2, 3, 2, ...
- ZeroMQ - 三种模型的python实现
ZeroMQ是一个消息队列网络库,实现网络常用技术封装.在C/S中实现了三种模式,这段时间用python简单实现了一下,感觉python虽然灵活.但是数据处理不如C++自由灵活. 1.Request- ...