swagger用于定义API文档。

 

好处:
1、前后端分离开发
2、API文档非常明确
3、测试的时候不需要再使用URL输入浏览器的方式来访问Controller
4、传统的输入URL的测试方式对于post请求的传参比较麻烦(当然,可以使用postman这样的浏览器插件)

5、spring-boot与swagger的集成简单的一逼

 

1、项目结构

4 springboot+swagger环境搭建_请求参数

2、pom.xml

 

[html]  view plain  copy
 
 
  1. <project xmlns="http:///POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http:///POM/4.0.0 http:///xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.   
  5.     <groupId>com.zzg</groupId>  
  6.     <artifactId>springbootone</artifactId>  
  7.     <version>0.0.1-SNAPSHOT</version>  
  8.     <packaging>jar</packaging>  
  9.   
  10.     <name>springbootone</name>  
  11.     <url>http://</url>  
  12.   
  13.     <properties>  
  14.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  15.     </properties>  
  16.   
  17.     <parent>  
  18.         <groupId>org.springframework.boot</groupId>  
  19.         <artifactId>spring-boot-starter-parent</artifactId>  
  20.         <version>1.4.3.RELEASE</version>  
  21.     </parent>  
  22.   
  23.     <dependencies>  
  24.         <dependency>  
  25.             <groupId>org.springframework.boot</groupId>  
  26.             <artifactId>spring-boot-starter-web</artifactId>  
  27.         </dependency>  
  28.         <dependency>  
  29.             <groupId>junit</groupId>  
  30.             <artifactId>junit</artifactId>  
  31.             <version>3.8.1</version>  
  32.             <scope>test</scope>  
  33.         </dependency>  
  34.         <!--引入swagger  -->  
  35.         <dependency>  
  36.            <groupId>io.springfox</groupId>  
  37.            <artifactId>springfox-swagger2</artifactId>  
  38.            <version>2.2.2</version>  
  39.         </dependency>  
  40.         <dependency>  
  41.            <groupId>io.springfox</groupId>  
  42.            <artifactId>springfox-swagger-ui</artifactId>  
  43.            <version>2.2.2</version>  
  44.         </dependency>  
  45.     </dependencies>  
  46. </project>  
3、Application.java

 

 

[html]  view plain  copy
 
 
  1. package com.zzg.springbootone;  
  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.     /**  
  13.      * @param args  
  14.      */  
  15.     public static void main(String[] args) {  
  16.         // TODO Auto-generated method stub  
  17.          SpringApplication.run(Application.class, args);  
  18.     }  
  19.   
  20. }  

 

说明:

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

 

[html]  view plain  copy
 
 
  1. package com.zzg.springbootone.controller;  
  2.   
  3. import io.swagger.annotations.Api;  
  4. import io.swagger.annotations.ApiImplicitParam;  
  5. import io.swagger.annotations.ApiImplicitParams;  
  6. import io.swagger.annotations.ApiOperation;  
  7. import io.swagger.annotations.ApiResponse;  
  8. import io.swagger.annotations.ApiResponses;  
  9.   
  10. import org.springframework.web.bind.annotation.RequestHeader;  
  11. import org.springframework.web.bind.annotation.RequestMapping;  
  12. import org.springframework.web.bind.annotation.RequestMethod;  
  13. import org.springframework.web.bind.annotation.RequestParam;  
  14. import org.springframework.web.bind.annotation.RestController;  
  15.   
  16. @RestController  
  17. @RequestMapping("/user")  
  18. @Api("userController相关api")  
  19. public class UserController {  
  20.      @ApiOperation("获取用户信息")  
  21.         @ApiImplicitParams({  
  22.             @ApiImplicitParam(paramType="query",name="username",dataType="String",required=true,value="用户的姓名",defaultValue="zhouzhigang"),  
  23.             @ApiImplicitParam(paramType="query",name="password",dataType="String",required=true,value="用户的密码",defaultValue="123456")  
  24.         })  
  25.         @ApiResponses({  
  26.             @ApiResponse(code=400,message="请求参数没填好"),  
  27.             @ApiResponse(code=404,message="请求路径没有或页面跳转路径不对")  
  28.         })  
  29.         @RequestMapping(value="/getUser",method=RequestMethod.GET)  
  30.         public void getUser(@RequestParam("username") String username, @RequestParam("password") String password) {  
  31.             System.out.println("username is:"+username);  
  32.             System.out.println("password is:"+password);  
  33.         }  
  34.   
  35. }  

 

说明:

  • @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这个注解不只是注解,还会影响运行期的程序,例子如下:

  4 springboot+swagger环境搭建_请求参数_02

如果ApiImplicitParam中的phone的paramType是query的话,是无法注入到rest路径中的,而且如果是path的话,是不需要配置ApiImplicitParam的,即使配置了,其中的value="手机号"也不会在swagger-ui展示出来。

 

具体其他的注解,查看:

https:///swagger-api/swagger-core/wiki/Annotations#apimodel

测试:

启动服务,浏览器输入"http://localhost:8080/swagger-ui.html"

4 springboot+swagger环境搭建_apache_03