第四章 springboot + swagger

注:本文参考自

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

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 @[email protected][email protected]
 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:抛出异常的类

以上这些就是最常用的几个注解了。

具体其他的注解,查看:

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!",查看响应内容:

时间: 2024-08-25 14:11:32

第四章 springboot + swagger的相关文章

第四章 springboot + swagger(转载)

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

第四章 SpringBoot配置文件

1.springboot配置文件 SpringBoot是基于约定的,所以很多配置都有默认值,但如果想使用自己的配置替换默认配置的话,就可以使用application.properties或者application.yml(application.yaml)进行配置. SpringBoot默认会从Resources目录下加载application.properties或application.yml(application.yaml)文件,其中,application.properties文件是键

第十四章 springboot + profile(不同环境读取不同配置)

具体做法: 不同环境的配置设置一个配置文件,例如:dev环境下的配置配置在application-dev.properties中:prod环境下的配置配置在application-prod.properties中. 在application.properties中指定使用哪一个文件 1.application-dev.properties(dev环境下的配置) 1 profile = dev_envrimont 2.application-prod.properties(prod环境下的配置)

第二十五章 springboot + hystrixdashboard

注意: hystrix基本使用:第十九章 springboot + hystrix(1) hystrix计数原理:附6 hystrix metrics and monitor 一.hystrixdashboard 作用: 监控各个hystrixcommand的各种值. 通过dashboards的实时监控来动态修改配置,直到满意为止 仪表盘: 二.启动hystrix 1.下载standalone-hystrix-dashboard-1.5.3-all.jar https://github.com/

第十六章 springboot + OKhttp + String.format

模拟浏览器向服务器发送请求四种方式: jdk原生的Http包下的一些类 httpclient(比较原始,不怎么用了):第一章 HttpClient的使用 Okhttp(好用,推荐) retrofit(好用,推荐),用法:第七章 springboot + retrofit 看本章之前可以先看看第七章 springboot + retrofit 1.myboot2项目 1.1.application.properties 1 server.port=8081 注意:指定服务器启动端口的有三种方式 在

第十二章 springboot + mongodb(复杂查询)

简单查询:使用自定义的XxxRepository接口即可.(见 第十一章 springboot + mongodb(简单查询)) 复杂查询:使用MongoTemplate以及一些查询条件构建类(BasicDBList.BasicDBObject.Criteria等) 1.application.properties 1 #mongodb note:mongo3.x will not use host and port,only use uri 2 #spring.data.mongodb.hos

第四章

第四章 源代码的下载和编译 1.下载.编译和测试Android源代码 配置Android源代码的下载环境 ①创建一个存放下载脚本文件(repo)的目录 # mkdir  ~/bin # PATH=~/bin:$PATH ②下载repo脚本文件 # curi http://dl-ssl.google.com/dl/googlesource/git-repo/repo > -/bin/repo # chmod a+x ~/bin/repo ③创建存放Android源代码的目录 # mkdir and

《UML精粹》 第四章 时序图

第四章 时序图 一般来说,我们会在一张时序图中画出某个情节的相关行为,图种会秀出这个使用案例(use case)里面可能出现的一些对象,以及在对象间传送的信息. 本章将通过一个简单情节,做时序图各方面的相关讨论.假设我们现在有一份订单,并且准备调用它的一个命令,算出这份订单的价格.为了达到这个目的,订单需要产看它里面所拥有的一些订单明细.决定它们的价格,价格决定方式是以订单明细中所包含产品之定价规则为基础决定的.对所有订单明细做完上述动作之后,接下来订单要算出整个折扣,这时候它是以跟客户绑在一起

Java 线程第三版 第四章 Thread Notification 读书笔记

一.等待与通知 public final void wait() throws InterruptedException 等待条件的发生. public final void wait(long timeout) throws InterruptedException 等待条件的发生.如果通知没有在timeout指定的时间内发生,它还是会返回. public final void wait(long timeout, int nanos) throws InterruptedException