微服务:springboot与swagger2的集成
现在测试都提倡自动化测试,那我们作为后台的开发人员,也得进步下啊,以前用postman来测试后台接口,那个麻烦啊,一个字母输错就导致测试失败,现在swagger的出现可谓是拯救了这些开发人员,便捷之处真的不是一点两点。下面我们看下如何在微服务中将springboot与swagger来结合吧。
1、swagger是什么,这个我觉得凡是一个开发人员就应该知道度娘啊,绝对强大。
简单说下,它的出现就是为了方便进行测试后台的restful形式的接口,实现动态的更新,当我们在后台的接口修改了后,swagger可以实现自动的更新,而不需要认为的维护这个接口进行测试。
2、springboot与swagger的集成:
第一步:jar包的引入:
<!-- swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
关于jar包的引入出现了一个问题就是版本的问题,可能需要与你的编辑器或者jdk要匹配吧,试了几个才最终成功导入jar。
第二步:swagger的配置启动类编写:
要使用swagger要进行一些配置,这个在界面的图上是可以显示的:类似于说明书:在这个类中我们会使用注解来进行启动swagger:

具体配置如下:
package com.springboot.example;
//swagger2的配置文件,在项目的启动类的同级文件建立
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 Swagger2 {
//swagger2的配置文件,这里可以配置swagger2的一些基本的内容,比如扫描的包等等
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//为当前包路径
.apis(RequestHandlerSelectors.basePackage("com.springboot.example.Controller"))
.paths(PathSelectors.any())
.build();
}
//构建 api文档的详细信息函数,注意这里的注解引用的是哪个
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
//页面标题
.title("Spring Boot 测试使用 Swagger2 构建RESTful API")
//创建人
.contact(new Contact("MarryFeng", "http://www.baidu.com", ""))
//版本号
.version("1.0")
//描述
.description("API 描述")
.build();
}
}
这里的坑是:所使用类的引入文件要注意到底是哪个,之前因为这个出错了,
|
1 2 |
|
|
1 2 |
|
第三步:使用swagger来进行模拟测试:
使用swagger2来进行测试接口主要是在哪些类中使用:这里我们依然选择在controller层:
package com.springboot.example.Controller;
import com.springboot.example.Service.StudentService;
import com.springboot.example.entity.Student;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
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;
/**
* Created by Administrator on 2017/9/13.
*/
@RestController
@RequestMapping("api")
@Api("swaggerDemoController相关的api")
public class SwaggerDemoController {
@Autowired
private StudentService studentService;
private static final Logger logger= LoggerFactory.getLogger(SwaggerDemoController.class);
@ApiOperation(value = "根据id查询学生信息", notes = "查询数据库中某个的学生信息")
@ApiImplicitParam(name = "id", value = "学生ID", paramType = "path", required = true, dataType = "Integer")
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public Student getStudent(@PathVariable int id) {
logger.info("开始查询某个学生信息");
return studentService.selectStudentById(id);
}
}
上面这些可以看下具体的注解是什么意思:
这样swagger2与springboot就集成完毕了。
看下最终效果吧:
访问路径:
http://localhost:8080/swagger-ui.html


输入id后,我们可以看到查询结果:、

是不是很方便,我们不用像postman一样来编写入口,swagger2自动完成:
而且实时更新:

是不是很方便!
至此swagger2与springboot的集成完毕。
转载:https://www.cnblogs.com/fengli9998/p/7522973.html
微服务:springboot与swagger2的集成的更多相关文章
- springboot与swagger2的集成
springboot与swagger2的集成 1.出现的背景 随着互联网技术的发展,现在的网站架构基本都由原来的后端渲染变成了:前端渲染.先后端分离的形态,而前端和后端的唯一联系,变成了API接口: ...
- 微服务学习二:springboot与swagger2的集成
现在测试都提倡自动化测试,那我们作为后台的开发人员,也得进步下啊,以前用postman来测试后台接口,那个麻烦啊,一个字母输错就导致测试失败,现在swagger的出现可谓是拯救了这些开发人员,便捷之处 ...
- 微服务springboot视频最新SpringBoot2.0.3版本技术视频教程【免费学习】
超火爆的springboot微服务技术怎么学,看这里,springboot超详细的教程↓↓↓↓↓↓https://ke.qq.com/course/179440?tuin=9b386640 01.sp ...
- 【下一代核心技术DevOps】:(五)微服务CI与Rancher持续集成
1. 引言 DevOps的核心魅力是快速的持续集成交付,降低研发和实施运维之间的交互,使得传统的各种扯皮现象统统消失.最重要的是降低成本 保障产品交付可靠性. 使用Rancher作为持续集成的关键环节 ...
- 微服务-springboot+websocket在线聊天室
一.引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- 微服务-springboot日志配置
springboot 默认会加载classpath:logback-spring.xml文件. springProfile 中的name名字对应application-xx.properties 中的 ...
- 微服务-springboot多环境配置(开发生产测试环境切换)
springboot根据spring.profiles.active会去寻找应该加载开发环境配置还是生产环境配置 application.properties #生产环境,开发环境,测试环境切换 pr ...
- Java微服务(Spring-boot+MyBatis+Maven)入门教程
1,项目创建 新建maven项目,如下图: 选择路径,下一步 输入1和2的内容,点完成 项目创建完毕,结构如下图所示: 填写pom.xml里内容,为了用于打包,3必须选择jar,4和5按图上填写 ...
- 微服务SpringBoot总结
什么是SpringBootSpringBoot是Spring项目中的一个子工程,与我们所熟知的Spring-framework 同属于spring的产品官方介绍:Spring Boot makes i ...
随机推荐
- vue播放mu38视频兼容谷歌ie等浏览器
<template> <div id="id_test_video" style="width:100%; height:auto;"> ...
- vue做页面按钮权限--分析
import * as types from '../mutation-types' const state = { btnCode: getBtnCode(), } const mutations ...
- PHP变量回收
PHP变量回收1 离开页面2 unset
- linux 把nginx加入到系统服务的方法
linux 把nginx加入到系统服务的方法一.首先写一个shell脚本,脚本名称:nginx<pre>#! /bin/bash# chkconfig: 35 85 15 # descri ...
- vue判断图片为空或者图片加载不成功时显示默认图片
纯css解决方案: <img src="broken.png" alt=""> img { position: relative; } img:af ...
- [Docker] - 不同容器之间相互访问的实现方式(例如:Client 访问 DB)
部署了两个独立的容器: Container #1 - Web ClientContainer #2 - SQL Server 不同容器间如何互访? 无法从 Container #1 访问到 Conta ...
- WEUI Search Input回车键无法跳转解决
现象:回车键依然当前页面, window.location.href 设置无法起作用 增加 window.event.returnValue = false; 解决问题
- Java设计RestfulApi接口,实现统一格式返回
创建返回状态码枚举 package com.sunny.tool.api.enums; /** * @Author sunt * @Description 响应枚举状态码 * @Date 2019/1 ...
- c,使用lib,dll
lib使用: #include "xxx.h" // lib的头文件 #pragma comment(lib, "xxx.lib") 这样会将lib里的数据编译 ...
- Mac Mini(late 2014) 添加NVMe固态组Fusion Drive
我买的是Mac Mini(late 2014)中配,内置5400转1T机械硬盘,该配置即使到了2019年安装macOS Mojave系统依旧是够用的,但硬盘严重拖累了运行的速度.之前考虑到更换内置sa ...