一、依赖:

       <dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.</version>
</dependency> <dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.</version>
</dependency>

二、新增配置项:

@Configuration
@EnableSwagger2
public class Swagger2 { @Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.bosssoft.platform.appframe.rest"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("应用中心springBoot之RestFulApi接口文档")
.version("1.0")
.build();
}
}

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

三、写生产文档的注解

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

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

/**
*
* @author Shaw
*
*/
@RestController
@RequestMapping(value = "/rest", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(value = "/rest", tags = "ApplicationRestController", description = "应用管理接口")
public class ApplicationRestController{ @Autowired
private AfaApplicationService applicationService; @ApiOperation("获取所有应用")
@RequestMapping(value="/application",method=RequestMethod.GET)
public List<ApiApplication> getApplications() {
AfaApplication app = new AfaApplication();
app.setIsOpen("");
List<AfaApplication> afaAppList = applicationService.getAfaApplicationList(app);
return BeanUtils.copyList(afaAppList, ApiApplication.class);
} @ApiOperation("根据用户编码获取当前用户应用")
@RequestMapping(value="/application/userCode/{userCode}",method=RequestMethod.GET)
public List<ApiApplication> getApplicationsByUserCode(@PathVariable("userCode")String userCode) {
return BeanUtils.copyList(applicationService.getApplicationsByUserCode(userCode),ApiApplication.class);
} }

启动项目,访问:

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

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

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

       <dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.</version>
</dependency> <dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.8.</version>
</dependency>

风格如下:

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. cJONS序列化工具解读二(数据解析)

    cJSON数据解析 关于数据解析部分,其实这个解析就是个自动机,通过递归或者解析栈进行实现数据的解析 /* Utility to jump whitespace and cr/lf *///用于跳过a ...

  2. bzoj1083: [SCOI2005]繁忙的都市 瓶颈生成树

    https://www.lydsy.com/JudgeOnline/problem.php?id=1083 题意:给你一个图,求生成树最大边权最小值 就是求瓶颈生成树(生成树中最大边权最小),最小生成 ...

  3. fis 前端构建工具

    1.http://fis.baidu.com/ (前端构建工具)

  4. Symmetrical Network Acceleration with EBS 12

    Andy Tremayne, my esteemed colleague and fellow blogger, has published a new whitepaper that discuss ...

  5. jquery动态创建元素 div元素随垂直滚动条位置变化置顶显示

    刚打开页面效果 拖动滑动条之后效果 页面代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" & ...

  6. 【后台测试】postman简介

    ◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/6266047.html 在接口测试的过程中,经常拿到一个接口 ...

  7. ES查询index对应的mapping信息

    private void getMappingByIndex(String indices) throws IOException { GetMappingsRequest getMappingsRe ...

  8. Appium Remote webdriver调用

    remote webdriver的模板 默认开启4723端口接受webdriver请求 默认开启4724用于和android通讯 #coding:utf-8 #Import the common pa ...

  9. MySQL学习(一): MySQL的初步操作与命令

    启动与关闭: 用管理员身份运行cmd 启动:net start mysql57 其中mysql57是我的MySQL名称,版本不同名称可能不一样 关闭:net stop mysql57 与上面同理 登录 ...

  10. 剑指offer--42.孩子们的游戏(圆圈中最后剩下的数)

    约瑟夫环,用链表,队列,总之模拟过程 ----------------------------------------------------------------- 时间限制:1秒 空间限制:32 ...