Swagger(一) SpringBoot整合Swagger2简单的例子

Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,参数和模型紧密集成到服务器端的代码,允许API来始终保持同步。Swagger 让部署管理和使用功能强大的API从未如此简单。

这里我给大家带来一个简单的整合DEMO 先来看项目结构

下面是基本的步骤

一.添加MAVEN依赖

[plain] view plain copy
  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>  
  11. <dependency>  
  12.     <groupId>org.codehaus.jackson</groupId>  
  13.     <artifactId>jackson-core-asl</artifactId>  
  14.     <version>1.9.13</version>  
  15. </dependency>  


二.编写Swagger配置类

[java] view plain copy
  1. @Configuration  
  2. @EnableSwagger2  
  3. public class Swaggers {  
  4.   
  5.     @Bean  
  6.     public Docket swaggerSpringMvcPlugin() {  
  7.         ApiInfo apiInfo = new ApiInfo("sample of springboot""sample of springboot"nullnullnullnullnull);  
  8.         Docket docket = new Docket(DocumentationType.SWAGGER_2).select().paths(regex("/user/.*")).build()  
  9.                 .apiInfo(apiInfo).useDefaultResponseMessages(false);  
  10.         return docket;  
  11.     }  
  12.   
  13.   
  14.     /*private ApiInfo apiInfo() { 
  15.         return new ApiInfoBuilder().title("测试API") 
  16.                 .description("樊亚的测试API1") 
  17.                 .version("1.0.0") 
  18.                 .build(); 
  19.     }*/  
  20.     /* @Bean 
  21.         public Docket createRestApi() { 
  22.             return new Docket(DocumentationType.SWAGGER_2) 
  23.                     .apiInfo(apiInfo()) 
  24.                     .select() 
  25.                     .apis(RequestHandlerSelectors.basePackage("com.didispace.web")) 
  26.                     .paths(regex("/user/.*")) 
  27.                     .build(); 
  28.         } 
  29.     */  
  30.   
  31. }  

当然也可以用链式编程的方法实现,这里我使用的是NEW

三.编写Controller

[java] view plain copy
  1. @RestController  
  2. @RequestMapping("/user")  
  3. @Api(value = "Shop")  
  4. public class SpringBootController {  
  5.   
  6.     @ApiOperation(value = "获取helloWorld", notes = "简单SpringMVC请求")  
  7.     @RequestMapping("/")  
  8.     String home() {  
  9.         return "HELLO WORLD";  
  10.     }  
  11.   
  12.     @ApiOperation(value = "获得A+B", notes = "根据url的classNo和url的studentName获得请求参数的字符串相加,RestFul风格的请求")  
  13.     @ApiImplicitParams({@ApiImplicitParam(name = "classNo", value = "班级编号", required = true, dataType = "String"),  
  14.     })  
  15.     @RequestMapping(value = "/class/{classNo}/to/{studentName}", method = RequestMethod.GET)  
  16.     String world(@PathVariable("classNo") String classNo, @PathVariable("studentName") String studentName) {  
  17.         return classNo + "  " + studentName;  
  18.     }  
  19.   
  20.   
  21. }  

四.编写Application载入类

[java] view plain copy
  1. @SpringBootApplication  
  2. public class Application {  
  3.     public static void main(String[] args) {  
  4.         SpringApplication.run(Application.class,args);  
  5.     }  
  6.   
  7. }  


Swagger会默认把所有Controller中的RequestMapping方法都生成API出来,实际上我们一般只需要标准接口的(像返回页面的那种Controller方法我们并不需要),所有你可以按下面的方法来设定要生成API的方法的要求。 


至此功能基本实现了,我们可以通过访问地址http://localhost:8080/swagger-ui.html/

查看生成好的API




附上Swagger相关的注解:

最常用的5个注解

@Api:修饰整个类,描述Controller的作用
@ApiOperation:描述一个类的一个方法,或者说一个接口
@ApiParam:单个参数描述
@ApiModel:用对象来接收参数
@ApiProperty:用对象接收参数时,描述对象的一个字段
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

其它若干

@ApiResponseHTTP响应其中1个描述
@ApiResponsesHTTP响应整体描述
@ApiIgnore:使用该注解忽略这个API 

@ApiClass
@ApiError
@ApiErrors

@ApiParamImplicit
@ApiParamsImplicit

这一期主要为了先把功能跑起来,下一期详解内部内容。

项目地址 :https://github.com/FANYA/Learn.git

参考博客:http://www.jianshu.com/p/8033ef83a8ed

                http://blog.csdn.net/catoop/article/details/50668896

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值