spring cloud-整合Swagger2构建RESTful服务的APIs
来源:互联网 发布:stcisp软件下载 编辑:程序博客网 时间:2024/06/07 16:13
前言
在前面的博客中,我们将服务注册到了Eureka上,可以从Eureka的UI界面中,看到有哪些服务已经注册到了Eureka Server上,但是,如果我们想查看当前服务提供了哪些RESTful接口方法的话,就无从获取了,传统的方法是梳理一篇服务的接口文档来供开发人员之间来进行交流,这种情况下,很多时候,会造成文档和代码的不一致性,比如说代码改了,但是接口文档没有改等问题,而Swagger2则给我们提供了一套完美的解决方案,下面,我们来看看Swagger2是如何来解决问题的。
一、引入Swagger2依赖的jar包
<!-- swagger2 --><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.2.2</version></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.2.2</version></dependency>二、初始化Swagger2的配置
@Configuration@EnableSwagger2 // 启用Swagger2public class Swagger2 { @Bean public Docket createRestApi() {// 创建API基本信息 return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.chhliu.jpa"))// 扫描该包下的所有需要在Swagger中展示的API,@ApiIgnore注解标注的除外 .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() {// 创建API的基本信息,这些信息会在Swagger UI中进行显示 return new ApiInfoBuilder() .title("Spring Boot中使用Swagger2构建RESTful APIs")// API 标题 .description("rdcloud-jpa提供的RESTful APIs")// API描述 .contact("chhliu@")// 联系人 .version("1.0")// 版本号 .build(); }}注:该配置类需要在Application同级目录下创建,在项目启动的时候,就初始化该配置类
三、完善API文档信息
public interface SonarControllerI {@ApiOperation(value="获取项目组Sonar对应的Url信息", notes="根据id获取项目组Sonar对应的Url信息")// 使用该注解描述接口方法信息 @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "SonarUrl表ID", required = true, dataType = "Long", paramType="path") })// 使用该注解描述方法参数信息,此处需要注意的是paramType参数,需要配置成path,否则在UI中访问接口方法时,会报错@GetMapping("/get/{id}")SonarUrl get(@PathVariable Long id);@ApiOperation(value="获取项目组Sonar对应的所有Url信息")@GetMapping("/get/all")List<SonarUrl> getAll();}
注:paramType表示参数的类型,可选的值为"path","body","query","header","form"
四、完善返回类型信息
@Entity(name = "SONAR_URL")public class SonarUrl implements Serializable {/** * */private static final long serialVersionUID = 1L;@ApiModelProperty(value="主键", hidden=false, notes="主键,隐藏", required=true, dataType="Long")// 使用该注解描述属性信息,当hidden=true时,该属性不会在api中显示@Id@GeneratedValue(strategy = GenerationType.AUTO)private Long id;@ApiModelProperty(value="URL链接地址")@Column(name="URL")private String url;@ApiModelProperty(value="项目组")@Column(name="TEAM")private String team;@ApiModelProperty(value="部门")@Column(name="DEPARTMENT")private String department; ……省略getter,setter方法…… }五、启动应用
1、在浏览器中输入:http://localhost:7622/swagger-ui.html
2、结果如下:
六、API文档访问与测试
Swagger除了提供API接口查看的功能外,还提供了调试测试功能
测试结果如下:
0 0
- spring cloud-整合Swagger2构建RESTful服务的APIs
- spring cloud-整合Swagger2构建RESTful服务的APIs
- Spring Boot中使用Swagger2构建RESTful APIs
- spring-cloud 构建一个restful的web服务
- Spring Boot中使用Swagger2构建RESTful APIS(含源码)
- 使用Swagger2生成spring boot应用RESTful APIs描述文档
- Spring Boot使用Swagger2构建RESTful文档
- Spring boot构建RESTFul API+使用Swagger2构建API文档
- 整合企业Spring Cloud分布式微服务云架构构建
- 整合spring cloud云服务架构代码结构构建
- Spring Boot中使用Swagger2构建强大的RESTful API文档
- Spring Boot中使用Swagger2构建强大的RESTful API文档
- Spring Boot中使用Swagger2构建强大的RESTful API文档
- Spring Boot中使用Swagger2构建强大的RESTful API文档
- Spring Boot中使用Swagger2构建强大的RESTful API文档
- Spring Boot中使用Swagger2构建强大的RESTful API文档
- Spring Boot中使用Swagger2构建强大的RESTful API文档
- Spring Boot中使用Swagger2构建强大的RESTful API文档
- springmvc的常见注解
- iOS理解“对象等同性”这一概念
- leecode 解题总结:275. H-Index II
- SpringMVC-学习笔记(1)——理解MVC及快速入门
- RMAN恢复完全恢复之部分数据文件丢失
- spring cloud-整合Swagger2构建RESTful服务的APIs
- POJ-1226 Substrings(后缀数组)
- Shiro 自定义realm授权与认证的实现
- Mac下sublime2 package control快捷安装
- ios NSNotificationCenter接受通知不能操作UI线程
- JQuery笔记(下)
- 《将博客搬至CSDN》
- 整理PHP $_SERVER 变量的详情说明
- Inside Real-Time Linux