一、依赖:

  1. <dependency>
  2. <groupId>io.springfox</groupId>
  3. <artifactId>springfox-swagger2</artifactId>
  4. <version>2.6.</version>
  5. </dependency>
  6.  
  7. <dependency>
  8. <groupId>io.springfox</groupId>
  9. <artifactId>springfox-swagger-ui</artifactId>
  10. <version>2.6.</version>
  11. </dependency>

二、新增配置项:

  1. @Configuration
  2. @EnableSwagger2
  3. public class Swagger2 {
  4.  
  5. @Bean
  6. public Docket createRestApi() {
  7. return new Docket(DocumentationType.SWAGGER_2)
  8. .apiInfo(apiInfo())
  9. .select()
  10. .apis(RequestHandlerSelectors.basePackage("com.bosssoft.platform.appframe.rest"))
  11. .paths(PathSelectors.any())
  12. .build();
  13. }
  14. private ApiInfo apiInfo() {
  15. return new ApiInfoBuilder()
  16. .title("应用中心springBoot之RestFulApi接口文档")
  17. .version("1.0")
  18. .build();
  19. }
  20. }

通过@Configuration注解,表明它是一个配置类,@EnableSwagger2开启swagger2。apiINfo()配置一些基本的信息。apis()指定扫描的包会生成文档。

三、写生产文档的注解

swagger通过注解表明该接口会生成文档,包括接口名、请求方法、参数、返回信息的等等。

@Api:修饰整个类,描述Controller的作用
@ApiOperation:描述一个类的一个方法,或者说一个接口
@ApiParam:单个参数描述
@ApiModel:用对象来接收参数
@ApiProperty:用对象接收参数时,描述对象的一个字段
@ApiResponse:HTTP响应其中1个描述
@ApiResponses:HTTP响应整体描述
@ApiIgnore:使用该注解忽略这个API
@ApiError :发生错误返回的信息
@ApiParamImplicitL:一个请求参数
@ApiParamsImplicit 多个请求参数
例如:

  1. /**
  2. *
  3. * @author Shaw
  4. *
  5. */
  6. @RestController
  7. @RequestMapping(value = "/rest", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  8. @Api(value = "/rest", tags = "ApplicationRestController", description = "应用管理接口")
  9. public class ApplicationRestController{
  10.  
  11. @Autowired
  12. private AfaApplicationService applicationService;
  13.  
  14. @ApiOperation("获取所有应用")
  15. @RequestMapping(value="/application",method=RequestMethod.GET)
  16. public List<ApiApplication> getApplications() {
  17. AfaApplication app = new AfaApplication();
  18. app.setIsOpen("");
  19. List<AfaApplication> afaAppList = applicationService.getAfaApplicationList(app);
  20. return BeanUtils.copyList(afaAppList, ApiApplication.class);
  21. }
  22.  
  23. @ApiOperation("根据用户编码获取当前用户应用")
  24. @RequestMapping(value="/application/userCode/{userCode}",method=RequestMethod.GET)
  25. public List<ApiApplication> getApplicationsByUserCode(@PathVariable("userCode")String userCode) {
  26. return BeanUtils.copyList(applicationService.getApplicationsByUserCode(userCode),ApiApplication.class);
  27. }
  28.  
  29. }

启动项目,访问:

  1. http://127.0.0.1:8088/appframe-web/swagger-ui.html

测试的时候,可以直接在这个ui界面上进行测试,或者可以通过postman或者soapui等工具进行测试。

备注可以通过修改依赖,将Ui的测试界面改成bootstrap风格的Api界面

  1. <dependency>
  2. <groupId>io.springfox</groupId>
  3. <artifactId>springfox-swagger2</artifactId>
  4. <version>2.6.</version>
  5. </dependency>
  6.  
  7. <dependency>
  8. <groupId>com.github.xiaoymin</groupId>
  9. <artifactId>swagger-bootstrap-ui</artifactId>
  10. <version>1.8.</version>
  11. </dependency>

风格如下:

  1. http://127.0.0.1:8088/appframe-web/doc.html

SpringBoot RestFul集成Swagger2的更多相关文章

  1. Springboot Oauth2 集成Swagger2权限验证实战

    Swagger是什么?能干什么?在这就不展开讲解了.本文主要讲解如何集成OAuth2的Password模式权限验证,验证接口是否具有权限. 引入依赖 <dependency> <gr ...

  2. SpringBoot中集成Swagger2

    1.依赖jar <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-s ...

  3. SpringBoot集成Swagger2实现Restful(类型转换错误解决办法)

    1.pom.xml增加依赖包 <dependency> <groupId>io.springfox</groupId> <artifactId>spri ...

  4. springboot集成swagger2构建RESTful API文档

    在开发过程中,有时候我们需要不停的测试接口,自测,或者交由测试测试接口,我们需要构建一个文档,都是单独写,太麻烦了,现在使用springboot集成swagger2来构建RESTful API文档,可 ...

  5. springboot 集成swagger2

    使用Swagger 可以动态生成Api接口文档,在项目开发过程中可以帮助前端开发同事减少和后端同事的沟通成本,而是直接参照生成的API接口文档进行开发,提高了开发效率.这里以springboot(版本 ...

  6. SpringBoot集成Swagger2在线文档

    目录 SpringBoot集成Swagger2在线文档 前言 集成SpringBoot 登录接口文档示例 代码 效果 注解说明 总结 SpringBoot集成Swagger2在线文档 前言 不得不说, ...

  7. 集成 Swagger2 构建强大的 RESTful API 文档

    微信公众号:一个优秀的废人如有问题或建议,请后台留言,我会尽力解决你的问题. 前言 快过年了,不知道你们啥时候放年假,忙不忙.反正我是挺闲的,所以有时间写 blog.今天给你们带来 SpringBoo ...

  8. Spring Boot2 系列教程 (四) | 集成 Swagger2 构建强大的 RESTful API 文档

    前言 快过年了,不知道你们啥时候放年假,忙不忙.反正我是挺闲的,所以有时间写 blog.今天给你们带来 SpringBoot 集成 Swagger2 的教程. 什么是 Swagger2 Swagger ...

  9. 【java框架】SpringBoot(3) -- SpringBoot集成Swagger2

    1.SpringBoot web项目集成Swagger2 1.1.认识Swagger2 Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体 ...

随机推荐

  1. hihocoder1457

    http://hihocoder.com/problemset/problem/1457 找不重复子串的和 topo序搞一搞,用父亲更新儿子节点的val,记得乘上节点数 //#pragma comme ...

  2. 1-22-shell脚本的基础

    1.1 shell 脚本的编写规范 1.2 变量与特殊变量应用 1.3局部变量与全局变量 1.4 条件测试表达式 ------------------------------------------- ...

  3. python - pandas或者sklearn中如何将字符形式的标签数字化

    参考:http://www.php.cn/wenda/91257.html https://www.cnblogs.com/king-lps/p/7846414.html http://blog.cs ...

  4. UVA-10972 RevolC FaeLoN (边双连通+缩点)

    题目大意:将n个点,m条边的无向图变成强连通图,最少需要加几条有向边. 题目分析:所谓强连通,就是无向图中任意两点可互达.找出所有的边连通分量,每一个边连通分量都是强连通的,那么缩点得到bcc图,只需 ...

  5. SqlServer中存储过程 returnC#代码处理以及对应的MySQL如何改写

    一.SqlServer 中 1. 创建表 create table testuser( id int, --primary key, names ), address ), paw ) ) 2.创建存 ...

  6. Docker的大坑小洼(一)

    Docker的大坑小洼 Posted on March 2, 2015March 2, 2015 by 孙宏亮 Docker成为云计算领域的新宠儿已经是不争的事实,作为高速发展的开源项目,难免存在这样 ...

  7. MySQL Batched Key Access

    Batched Key Access是MySQL 5.6 版本中的新特性,是一种用户提高表join性能的算法.[Batched Key Access]       对于多表join语句,当MySQL使 ...

  8. 内存保护机制及绕过方法——利用Ret2Libc绕过DEP之VirtualProtect函数

    利用Ret2Libc绕过DEP之VirtualProtect函数 ⑴.  原理分析: i.相关概念: VirtualProtect()函数: BOOL WINAPI VirtualProtect( _ ...

  9. LeetCode OJ:LRU Cache(最近使用缓存)

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  10. QT帮助文档 英文

    http://doc.qt.io/archives/qtquick-components-symbian-1.1/qml-button.html