现在测试都提倡自动化测试,那我们作为后台的开发人员,也得进步下啊,以前用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

@Configuration

@EnableSwagger2<br>这两个注解,一个是swagger2的配置,一个是项目启动的时候启动swagger2.<br>具体什么意思看下代码就知道了。

1

2

//为当前包路径

<span style="color: #ff0000">.apis(RequestHandlerSelectors.basePackage("com.springboot.example.Controller"))</span><br><span style="color: #ff0000">这个包指的是我们在哪些类中使用swagger2来测试。</span>

第三步:使用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的集成的更多相关文章

  1. springboot与swagger2的集成

    springboot与swagger2的集成 1.出现的背景 随着互联网技术的发展,现在的网站架构基本都由原来的后端渲染变成了:前端渲染.先后端分离的形态,而前端和后端的唯一联系,变成了API接口: ...

  2. 微服务学习二:springboot与swagger2的集成

    现在测试都提倡自动化测试,那我们作为后台的开发人员,也得进步下啊,以前用postman来测试后台接口,那个麻烦啊,一个字母输错就导致测试失败,现在swagger的出现可谓是拯救了这些开发人员,便捷之处 ...

  3. 微服务springboot视频最新SpringBoot2.0.3版本技术视频教程【免费学习】

    超火爆的springboot微服务技术怎么学,看这里,springboot超详细的教程↓↓↓↓↓↓https://ke.qq.com/course/179440?tuin=9b386640 01.sp ...

  4. 【下一代核心技术DevOps】:(五)微服务CI与Rancher持续集成

    1. 引言 DevOps的核心魅力是快速的持续集成交付,降低研发和实施运维之间的交互,使得传统的各种扯皮现象统统消失.最重要的是降低成本 保障产品交付可靠性. 使用Rancher作为持续集成的关键环节 ...

  5. 微服务-springboot+websocket在线聊天室

    一.引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  6. 微服务-springboot日志配置

    springboot 默认会加载classpath:logback-spring.xml文件. springProfile 中的name名字对应application-xx.properties 中的 ...

  7. 微服务-springboot多环境配置(开发生产测试环境切换)

    springboot根据spring.profiles.active会去寻找应该加载开发环境配置还是生产环境配置 application.properties #生产环境,开发环境,测试环境切换 pr ...

  8. Java微服务(Spring-boot+MyBatis+Maven)入门教程

    1,项目创建    新建maven项目,如下图: 选择路径,下一步 输入1和2的内容,点完成 项目创建完毕,结构如下图所示: 填写pom.xml里内容,为了用于打包,3必须选择jar,4和5按图上填写 ...

  9. 微服务SpringBoot总结

    什么是SpringBootSpringBoot是Spring项目中的一个子工程,与我们所熟知的Spring-framework 同属于spring的产品官方介绍:Spring Boot makes i ...

随机推荐

  1. mysql:获取某个表的所有字段

    select COLUMN_NAME from information_schema.COLUMNS where table_name = '表名' and table_schema = '数据库名' ...

  2. maven cloudara依赖下载

    最近开发的项目使用到了cloudara的依赖,已经在pom.xml 中配置了cloudara的repository,但是还是无法下载 <repositories> <reposito ...

  3. 在ensp中的acl控制

    原理 实验模拟 实验拓扑 相关参数 我们在每一台路由器上设置ospf服务,使其互相能通 下面我们配置基本ACL控制访问 配置完成后,尝试在R1上建立telent连接 但是这样设置是不安全的,只要是直连 ...

  4. SET key value [EX seconds] [PX milliseconds] [NX|XX]

    SET key value [EX seconds] [PX milliseconds] [NX|XX] 可用版本: >= 1.0.0 时间复杂度: O(1) 将字符串值 value 关联到 k ...

  5. [Linux] 树莓派 4B 安装 Ubuntu 19.10 (Eoan Ermine) IOT 版

    硬件:Raspberry Pi 4B系统:Ubuntu 19.10 (Eoan Ermine) for IOT官网:https://ubuntu.com/download/iot/raspberry- ...

  6. mysql 多个字段的查询处理

    https://blog.csdn.net/zzzgd_666/article/details/81101548

  7. c++ pipe实现父子进程通信

    1.父子进程通信pipe编程流程 -创建管道 -设置进程的输出到管道 -创建进程 -关闭管道写句柄 -读管道读句柄,把数据读到一个buffer里 2.注意事项 -读管道数据的时候,一定要关闭写句柄: ...

  8. (十一)pdf的构成之文件尾

    件尾部(trailer)如何找到交叉引用表和其他特殊对象 属性: / Size [integer]:指定交叉引用表中的条目数(也计算更新部分中的对象).使用的数字不应是间接参考. / Prev [in ...

  9. 为 WPF 程序添加 Windows 跳转列表的支持

    原文:为 WPF 程序添加 Windows 跳转列表的支持 Windows 跳转列表是自 Windows 7 时代就带来的功能,这一功能是跟随 Windows 7 的任务栏而发布的.当时应用程序要想用 ...

  10. win10 mars xlog编译

    win10 mars xlog编译   一. 环境准备 安装 cmake 以及 python2.7, 以及下载 ndk-r16b,并配置环境变量 NDK_ROOT 指向 ndk 路径. 如果是 Win ...