第四章 springboot + swagger

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介: 注:本文参考自 http://www.jianshu.com/p/0465a2b837d2   swagger用于定义API文档。 好处: 前后端分离开发 API文档非常明确 测试的时候不需要再使用URL输入浏览器的方式来访问Controller 传统的输入URL的测试方式对于post请求的传参比较麻烦(当然,可以使用postman这样的浏览器插件) spring-boot与swagger的集成简单的一逼 1、项目结构 和上一节一样,没有改变。

注:本文参考自

http://www.jianshu.com/p/0465a2b837d2

 

swagger用于定义API文档。

好处:

  • 前后端分离开发
  • API文档非常明确
  • 测试的时候不需要再使用URL输入浏览器的方式来访问Controller
  • 传统的输入URL的测试方式对于post请求的传参比较麻烦(当然,可以使用postman这样的浏览器插件)
  • spring-boot与swagger的集成简单的一逼

1、项目结构

和上一节一样,没有改变。

2、pom.xml

引入了两个jar。

按 Ctrl+C 复制代码
按 Ctrl+C 复制代码

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:参数的默认值
  • @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

 

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore     ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库 ECS 实例和一台目标数据库 RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
1月前
|
Java API Spring
【一】springboot整合swagger
【一】springboot整合swagger
32 0
|
1月前
|
XML Java 测试技术
【二】springboot整合自定义swagger
【二】springboot整合自定义swagger
21 0
|
3月前
|
Java API Spring
Swagger使用-Spring Boot整合Swagger
Swagger使用-Spring Boot整合Swagger
45 0
|
1月前
|
Java Spring
Spring Boot3整合knife4j(swagger3)
Spring Boot3整合knife4j(swagger3)
131 1
|
2月前
|
Java API
SpringBoot 整合swagger3.X
SpringBoot 整合swagger3.X
|
2月前
|
Java 应用服务中间件 网络安全
Nginx配置静态页面+springboot应用+swagger+SSL的实现
Nginx配置静态页面+springboot应用+swagger+SSL的实现
|
2月前
|
Java Maven
【SpringBoot专题_02】springboot集成Swagger详细教程
【SpringBoot专题_02】springboot集成Swagger详细教程
|
4月前
|
Java API
SpringBoot中的Swagger2如何使用?
SpringBoot中的Swagger2如何使用?
21 1
|
4月前
|
Java API
SpringBoot集成Swagger2组件
SpringBoot集成Swagger2组件
28 1
|
4月前
|
前端开发 搜索推荐 Java
springboot集成swagger knife4j 最详细的步骤 手把手教你继承swagger
springboot集成swagger knife4j 最详细的步骤 手把手教你继承swagger
243 0
springboot集成swagger knife4j 最详细的步骤 手把手教你继承swagger