Spring Boot + Swagger
前言:
在互联网公司, 微服务的使用者一般分为两种, 客户端和其他后端项目(包括关联微服务),不管是那方对外提供文档 让别人理解接口 都是必不可少的。传统项目中一般使用wiki或者文档, 修改繁琐,调用方不一定及时了解变化。 微服务时代,效率第一,使用Swagger可以在部署的时候生成在线文档,同时UI也特别漂亮清晰,可谓提供api之利器,Swagger 让部署管理和使用功能强大的API从未如此简单。网上Swagger文章不少, 但是少有跟SpringBoot集成,故而来一篇,造福社会.
注:本文参考自
http://www.jianshu.com/p/0465a2b837d2
swagger用于定义API文档。
详情参考:Swagger简介
好处:
- 前后端分离开发
- 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!",查看响应内容:
Reference:
http://blog.csdn.net/haoyifen/article/details/52703376
springfox官方文档: https://springfox.github.io/springfox/docs/snapshot/#introduction
Spring Boot + Swagger的更多相关文章
- spring boot + swagger + mysql + maven
1.首先编写 yaml 文件,创建项目所需的接口,在swagger.io官网上生成 spring boot项目: 2.由于生成的spring boot项目是公共类的所以还需要修改成所需的项目名称,主要 ...
- spring boot Swagger 集成
1. pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://ww ...
- Spring Boot : Swagger 2
每次修改完代码需要找原本的API时楼主的内心是痛苦的,因为一般情况下都找不到,需要重新写一份.如果使用Swagger的话,只要加几个注解就可以实时生成最新的在线API文档,而且不仅仅是文档,同时支持A ...
- Spring Boot --- Swagger基本使用
1. pom <!-- swagger2 --> <dependency> <groupId>io.springfox</groupId> <ar ...
- Spring Boot 集成Swagger
Spring Boot 集成Swagger - 小单的博客专栏 - CSDN博客https://blog.csdn.net/catoop/article/details/50668896 Spring ...
- Spring Boot项目简单上手+swagger配置+项目发布(可能是史上最详细的)
Spring Boot项目简单上手+swagger配置 1.项目实践 项目结构图 项目整体分为四部分:1.source code 2.sql-mapper 3.application.properti ...
- HTTP RESTful服务开发 spring boot+Maven +Swagger
这周配合第三方平台整合系统,需要提供HTTP REST服务和使用ActiveMQ推送消息,研究了下,做个笔记. 1.使用eclipse创建Spring Boot项目 创建Spring Boot项目( ...
- spring boot swagger-ui.html 404
很奇怪的问题,找了好久. 因为spring boot+swagger实现起来很简单.看下面三部曲: 1.pom添加两个swagger依赖. <!-- Swagger依赖包 --> < ...
- Spring boot中使用springfox来生成Swagger Specification小结
Rest接口对应Swagger Specification路径获取办法: 根据location的值获取api json描述文件 也许有同学会问,为什么搞的这么麻烦,api json描述文件不就是h ...
随机推荐
- Codeforces Round #613 Div.1 D.Kingdom and its Cities 贪心+虚树
题目链接:http://codeforces.com/contest/613/problem/D 题意概述: 给出一棵树,每次询问一些点,计算最少删除几个点可以让询问的点两两不连通,无解输出-1.保证 ...
- Scrum立会报告+燃尽图(十月二十三日总第十四次)
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2246 项目地址:https://git.coding.net/zhang ...
- Scrum立会报告+燃尽图(Beta阶段第四次)
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2386 项目地址:https://coding.net/u/wuyy694 ...
- 博弈---威佐夫博奕(Wythoff Game)
这个写的不错 威佐夫博奕(Wythoff Game):有两堆各若干个物品,两个人轮流从某一堆或同 时从两堆中取同样多的物品,规定每次至少取一个,多者不限,最后取光者得胜. 这种情况下是颇为复杂 ...
- ueditor与mvc4中坑 -编辑时显示源码问题
最近一次使用 ueditor 时,在MVC中的修改新闻内容时发现,怎么调用都是编辑器保存下来的源码,代码如下 <script id="ucontent" name=" ...
- C#使用 SharpSSH
准备试一把监控Linux机器 . 附件如下 :http://files.cnblogs.com/files/lclblog/Tamir.SharpSsh.zip
- Collections带有的排序方法 传入的元素类型 需是子类或者这个类的实例
- HDU3710-Battle Over Cities
题意 给出一个\(n\)个点\(m\)条边的无向连通图,问删掉每一个点后的最小生成树权值和为多少(如果不存在最下生成树就输出inf). \(n\le 2\times 10^4,m\le 10^5\) ...
- CodeChef KnightMov
码死了...考试的时候基本上是写一会儿思考一会儿人生....考完了调了调...最后400行+....不应该这么长的....以后重写一下再补题解..... 也许这就是蒟蒻吧.jpg 安利cstdio博客 ...
- Java开发Excel POI getPhysicalNumberOfCells 与 getLastCellNum的区别
1.getPhysicalNumberOfCells 与 getLastCellNum的区别 用org.apache.poi的包做excel导入,无意间发明若是excel文件中有空列,空列后面的数据全 ...