将Swagger与Spring Boot REST API集成
在上一篇文章中,我谈到了我使用Spring Boot创建RESTFul Services的经验。 在创建REST API时,正确的文档是其中的必需部分。
昂首阔步是什么?
Swagger (Swagger 2)是用于描述和记录REST API的规范。 它指定了REST Web服务的格式,包括URL,资源,方法等。Swagger将根据应用程序代码生成文档,并处理渲染部分。
在本文中,我将把Swagger 2文档集成到基于Spring Boot的REST Web服务中。 因此,我将使用Springfox实现来生成swagger文档。 如果您想知道如何运行/构建Spring Boot项目,请参考我以前的文章。
Springfox提供了两个依赖关系来生成API Doc和Swagger UI。 如果您不希望将Swagger UI集成到您的API级别,则无需添加Swagger UI依赖项。
<dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.7.0</version></dependency>
<dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.7.0</version></dependency>
@ EnableSwagger2注释在类中启用Springfox Swagger支持。 为了记录服务,Springfox使用了Docket。 Docket帮助配置了要记录的服务子集,并按名称将其分组,等等。最隐蔽的概念是,Springfox通过使用基于Spring配置的API语义在运行时检查应用程序来工作。 换句话说,您必须创建一个使用spring的@Configuration的Spring Java Configuration类。
在我的示例中,我根据添加的RestController类生成了庞大的文档。
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;@Configuration
@EnableSwagger2
public class ApplicationConfig {@Beanpublic Docket api() {return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.basePackage("com.chandana.helloworld.controllers")).paths(PathSelectors.any()).build();}
}
由于我添加了两个控制器,因此这将分别对每个控制器相关的API进行分组(标记)。
开箱即用,Springfox提供了五个谓词,它们是任何类,没有类,具有ClassAnnotation,withMethodAnnotation和basePackage。
养蜂信息
Swagger提供了一些默认值,例如“ API文档”,“通过联系电子邮件创建”,“ Apache 2.0”。 因此,您可以通过添加apiInfo(ApiInfo apiInfo)方法来更改这些默认值。 ApiInfo类包含有关API的自定义信息。
@Beanpublic Docket api() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(getApiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.chandana.helloworld.controllers")).paths(PathSelectors.any()).build();}private ApiInfo getApiInfo() {Contact contact = new Contact("Chandana Napagoda", "http://blog.napagoda.com", "cnapagoda@gmail.com");return new ApiInfoBuilder().title("Example Api Title").description("Example Api Definition").version("1.0.0").license("Apache 2.0").licenseUrl("http://www.apache.org/licenses/LICENSE-2.0").contact(contact).build();}
添加ApiInfo后,生成的文档将类似于以下内容:
控制器和POJO级别文档
@Api注释用于解释每个rest控制器类。
@ApiOperation批注用于解释以描述资源和方法。
@ApiResponse批注用于解释描述可以由操作返回的其他响应。例如:200 ok或202接受,等等。
@ApiModelProperty批注描述POJO(Bean)类的属性。
添加上述注释后,最终生成的swagger文档如下所示:
Spring RestController类:
package com.chandana.helloworld.controllers;import com.chandana.helloworld.bean.Greeting;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/api")
@Api(value = "user", description = "Rest API for user operations", tags = "User API")
public class HelloWorldController {@RequestMapping(value = "/hello/{name}", method = RequestMethod.GET, produces = "application/json")@ApiOperation(value = "Display greeting message to non-admin user", response = Greeting.class)@ApiResponses(value = {@ApiResponse(code = 200, message = "OK"),@ApiResponse(code = 404, message = "The resource not found")})public Greeting message(@PathVariable String name) {Greeting msg = new Greeting(name, "Hello " + name);return msg;}
}
问候模型类:
package com.chandana.helloworld.bean;import io.swagger.annotations.ApiModelProperty;public class Greeting {@ApiModelProperty(notes = "Provided user name", required =true)private String player;@ApiModelProperty(notes = "The system generated greeting message" , readOnly =true)private String message;public Greeting(String player, String message) {this.player = player;this.message = message;}public String getPlayer() {return player;}public void setPlayer(String player) {this.player = player;}public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}
}
AppConfig类:
package com.chandana.helloworld.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;@Configuration
@EnableSwagger2
public class ApplicationConfig {@Beanpublic Docket api() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(getApiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.chandana.helloworld.controllers")).paths(PathSelectors.any()).build();}private ApiInfo getApiInfo() {Contact contact = new Contact("Chandana Napagoda", "http://blog.napagoda.com", "cnapagoda@gmail.com");return new ApiInfoBuilder().title("Example Api Title").description("Example Api Definition").version("1.0.0").license("Apache 2.0").licenseUrl("http://www.apache.org/licenses/LICENSE-2.0").contact(contact).build();}
}
您也可以从我的GitHub存储库下载Swagger Spring Boot Project源代码 。
翻译自: https://www.javacodegeeks.com/2017/09/integrating-swagger-spring-boot-rest-api.html
将Swagger与Spring Boot REST API集成相关推荐
- Spring Boot最新版集成邮件发送功能大全
Spring Boot最新版集成邮件发送功能大全 前言 一.开启SMTP服务并获取授权码 二.创建Spring Boot项目 1.配置邮箱基本信息: 2.简单邮件发送: 3.发送带附件的邮件: 5.使 ...
- Spring Boot 极简集成 Shiro
点击关注公众号,Java干货及时送达 1. 前言 Apache Shiro是一个功能强大且易于使用的Java安全框架,提供了认证,授权,加密,和会话管理. Shiro有三大核心组件: Subject: ...
- spring boot、mybatis集成druid数据库连接池,实现mysql cluster HA负载均衡访问
spring boot.mybatis集成druid数据库连接池,实现mysql cluster HA负载均衡访问 1.原理实现介绍 本质来说使用连接池是为了节省创建.关闭数据库连接的资源消耗,从而提 ...
- 10、Spring Boot 2.x 集成 Log4j2
1.10 Spring Boot 2.x 集成 Log4j2 完整源码: Spring-Boot-Demos 转载于:https://www.cnblogs.com/Grand-Jon/p/99980 ...
- Spring Boot项目中集成Elasticsearch,并实现高效的搜索功能
Spring Boot项目中集成Elasticsearch 前言 环境准备 引入依赖 配置Elasticsearch连接信息 定义实体类 定义Elasticsearch操作接口 实现搜索功能 总结 前 ...
- Spring Data REST API集成Springfox、Swagger
原文: Documenting a Spring Data REST API with Springfox and Swagger 使用Spring Date REST,你可以迅速为Spring Da ...
- 使用基本身份验证来保护Spring Boot REST API
这是我的Spring Boot Blog帖子系列的第三篇文章. 在第一篇文章中,我谈到了我使用Spring Boot创建RESTFul Services的经验. 然后我将样本扩展到 与Swagger文 ...
- 用Spring Boot开发API请求详解--API开发
那么,如何写一套漂亮的API接口呢? 本次我们先了解一下Spring对API接口开发的支持,然后我们采用Spring Boot搭建项目,借用Swagger2列出API接口,便于查阅. 返回格式 API ...
- springboot 学习十五:Spring Boot 优雅的集成Swagger2、Knife4j
文章目录 导言 一.swagger2介绍 二.springBoot-swagger2实战演练 1. 快速创建项目 2. 引入是swagger2 依赖 3. swagger2 常用注解 4. ==配置 ...
最新文章
- Linux那些事儿 之 戏说USB(31)驱动的生命线(三)
- 程序员必知的操作系统知识点
- 手持机设备公司(WINCE/ANDROID/LINUX)
- 新手第一课-什么是深度学习
- python导入urllib request_Python 3.3 - urllib.request - 导入错误
- how is odata metadata request served 故意把configuration file里的GM6改成GM61之后
- Pytorch中model.eval()的作用分析
- 【Flink】Flink 写入 AnalyticDB MySQL
- 用python计算长方体的体积用什么函数_python处理DICOM并计算三维模型体积
- geekbench 3 android,Geekbench列表声称Xperia 3将包含12GB RAM并运行Android 10
- 【优化布局】基于matlab GUI遗传算法求解PCB元器件布局优化问题【含Matlab源码 694期】
- 【使用R语言两行语句将搜狗词库转为csv格式】
- 谷歌地球替代软件、谷歌街景、谷歌三维城市模型查看全方案
- 利用python爬虫程序爬取豆瓣影评
- 大学计算机Excel考试内容,大学计算机考试—Excel(操作题供参考,不附练习文件)...
- jxl生成excel 设置隐藏列 隐藏行
- LSB算法修改了原始图像的哪些值--底层分析
- 艾司博讯:拼多多怎么设置团长ID?团长权限?
- 【5年Android从零复盘系列之二十八】Android存储(3):assets文件详解
- Secondary NameNode:它究竟有什么作用?
热门文章
- JavaFX其他事件
- 这些面试中的智力题,你都会了吗
- vue2.0 $router和$route的区别
- 点滴小组KTV点歌系统简介
- springmvc报错 nested exception is org.mybatis.spring.MyBatisSystemException:
- Redis(案例五:Set数据)
- 查询空值中的注意事项
- Java Poi 向excel中插入图片
- 修改Sublime Text3 的侧边栏字体大小
- 可视化大屏设计尺寸_可视化大屏设计_酷炫不是最高效的大屏展示的唯一标准...