Swagger是一个用于描述和测试restful接口的工具,只要在定义restful接口时增加一些类和方法的描述注解,通过很简单的配置就可以得到一个展示接口定义页面,也可以在页面上设置参数提交测试接口(替代postman的部分功能)。

接口修改后不需要单独修改描述文档,swagger自动生成接口文档。下面讲一下如果搭建一个最简单swagger测试Demo。

一、创建一个SpringBoot的maven项目

项目创建方式可以参考我这篇博客《Spring Boot初探之restful服务发布》,

项目创建后的目录;

二、创建好后在pom.xml文件中增加swagger依赖的包

  1. <dependency>
  2.  
  3. <groupId>io.springfox</groupId>
  4.  
  5. <artifactId>springfox-swagger2</artifactId>
  6.  
  7. <version>2.8.0</version>
  8.  
  9. </dependency>
  10.  
  11. <dependency>
  12.  
  13. <groupId>io.springfox</groupId>
  14.  
  15. <artifactId>springfox-swagger-ui</artifactId>
  16.  
  17. <version>2.8.0</version>
  18.  
  19. </dependency>

直接使用appache的仓库:

  1. <repository>
  2. <id>springfox-swagger</id>
  3. <url>https://mvnrepository.com/artifact/io.springfox/springfox-swagger2</url>
  4. </repository>
  5. <repository>
  6. <id>springfox-swagger-ui</id>
  7. <url>https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui</url>
  8. </repository>

三、添加swagger的配置加载类(Swagger2Config.java)

  1. package com.elon.springbootdemo.config;
  2.  
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
  6.  
  7. import springfox.documentation.builders.ApiInfoBuilder;
  8. import springfox.documentation.builders.PathSelectors;
  9. import springfox.documentation.builders.RequestHandlerSelectors;
  10. import springfox.documentation.service.ApiInfo;
  11. import springfox.documentation.spi.DocumentationType;
  12. import springfox.documentation.spring.web.plugins.Docket;
  13. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  14.  
  15. @Configuration
  16. @EnableSwagger2
  17. public class Swagger2Config extends WebMvcConfigurerAdapter {
  18. @Bean
  19. public Docket api() {
  20. return new Docket(DocumentationType.SWAGGER_2)
  21. .select()
  22. .apis(RequestHandlerSelectors.basePackage("com.elon.springbootdemo.ws"))
  23. .paths(PathSelectors.any())
  24. .build()
  25. .apiInfo(getApiInfo());
  26. }
  27.  
  28. private ApiInfo getApiInfo()
  29. {
  30. ApiInfo apiInfo = new ApiInfoBuilder().title("用户管理模块")
  31. .description("定义用户数据的增加、删除、修改接口")
  32. .termsOfServiceUrl("http://www.cnblogs.com/elon")
  33. .version("1.0")
  34. .build();
  35. return apiInfo;
  36. }
  37. }

四、添加用于测试的restful接口(WSUserSwagger.java)

  1. package com.elon.springbootdemo.ws;
  2.  
  3. import org.springframework.web.bind.annotation.PathVariable;
  4. import org.springframework.web.bind.annotation.RequestBody;
  5. import org.springframework.web.bind.annotation.RequestHeader;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RequestMethod;
  8. import org.springframework.web.bind.annotation.RequestParam;
  9. import org.springframework.web.bind.annotation.RestController;
  10.  
  11. import io.swagger.annotations.Api;
  12. import io.swagger.annotations.ApiOperation;
  13.  
  14. @RestController
  15. @RequestMapping(value="swagger-demo")
  16. @Api(value="WSUserSwagger", description="用户信息管理")
  17. public class WSUserSwagger {
  18.  
  19. @ApiOperation(value="添加用户", notes="添加用户")
  20. @RequestMapping(value="/v1/user", method=RequestMethod.POST)
  21. public String addUser(@RequestBody String userInfo) {
  22. return "Add user:" + userInfo;
  23. }
  24.  
  25. @ApiOperation(value = "根据名称查询用户", notes = "根据名称查询用户")
  26. @RequestMapping(value = "/v1/user", method = RequestMethod.GET)
  27. public String queryUserByName(@RequestParam("name") String name, @RequestHeader("age") int age) {
  28. return name + age;
  29. }
  30.  
  31. @ApiOperation(value="删除用户", notes="删除用户")
  32. @RequestMapping(value="/v1/user/{name}", method=RequestMethod.DELETE)
  33. public String deleteUser(@PathVariable("name") String name) {
  34. return "delete " + name;
  35. }
  36. } 

五、启动后测试

在浏览器中输入 http://localhost:8080/swagger-ui.html#/, 打开页面可以看到定义的接口:

测试GET方法,点”Try it out”后输入参数, 点击”execute”执行可以看到接口执行后的返回结果。

SpringBoot初探之Swagger配置的更多相关文章

  1. springboot+mybatis-puls利用swagger构建api文档

    项目开发常采用前后端分离的方式.前后端通过API进行交互,在Swagger UI中,前后端人员能够直观预览并且测试API,方便前后端人员同步开发. 在SpringBoot中集成swagger,步骤如下 ...

  2. springboot+jpa+mysql+swagger整合

    Springboot+jpa+MySQL+swagger整合 创建一个springboot web项目 <dependencies> <dependency>      < ...

  3. Spring Boot项目简单上手+swagger配置+项目发布(可能是史上最详细的)

    Spring Boot项目简单上手+swagger配置 1.项目实践 项目结构图 项目整体分为四部分:1.source code 2.sql-mapper 3.application.properti ...

  4. SpringBoot集成Swagger2并配置多个包路径扫描

    1. 简介   随着现在主流的前后端分离模式开发越来越成熟,接口文档的编写和规范是一件非常重要的事.简单的项目来说,对应的controller在一个包路径下,因此在Swagger配置参数时只需要配置一 ...

  5. SpringBoot 优雅整合Swagger Api 自动生成文档

    前言 一个好的可持续交付的项目,项目说明,和接口文档是必不可少的,swagger api 就可以帮我们很容易自动生成api 文档,不需要单独额外的去写,无侵入式,方便快捷大大减少前后端的沟通方便查找和 ...

  6. springboot 入门八-自定义配置信息(编码、拦截器、静态资源等)

    若想实际自定义相关配置,只需要继承WebMvcConfigurerAdapter.WebMvcConfigurerAdapter定义些空方法用来重写项目需要用到的WebMvcConfigure实现.具 ...

  7. springboot情操陶冶-web配置(九)

    承接前文springboot情操陶冶-web配置(八),本文在前文的基础上深入了解下WebSecurity类的运作逻辑 WebSecurityConfigurerAdapter 在剖析WebSecur ...

  8. springboot情操陶冶-web配置(七)

    参数校验通常是OpenApi必做的操作,其会对不合法的输入做统一的校验以防止恶意的请求.本文则对参数校验这方面作下简单的分析 spring.factories 读者应该对此文件加以深刻的印象,很多sp ...

  9. springboot情操陶冶-web配置(四)

    承接前文springboot情操陶冶-web配置(三),本文将在DispatcherServlet应用的基础上谈下websocket的使用 websocket websocket的简单了解可见维基百科 ...

随机推荐

  1. 织梦调用seotitle

    如果有seotitle则调用seotitle,没有则调用title {dede:field.array runphp='yes'} if(@me['seotitle']=='') {@me=@me[' ...

  2. 基于jquery的城市选择插件

    城市选择插件的难度不是很大,主要是对dom节点的操作.而我写的这个插件相对功能比较简答,没有加入省市联动. 上代码好了,参照代码的注释应该比较好理解. /* *基于jquery的城市选择插件 *aut ...

  3. SpringMvc解决Restful中文乱码问题

    中文乱码问题解决方式: <!-- 解决中文乱码问题 --> <filter> <filter-name>CharacterEncodingFilter</fi ...

  4. Hibernate学习(二)保存数据

    package cn.lonecloud.test; import java.util.Date; import org.hibernate.HibernateException; import or ...

  5. SmileyFace——基于OpenCV的人脸人眼检测、面部识别程序

    项目地址 https://github.com/guoyaohua/SmileyFace 开发环境 Visual Studio 2010 MFC + OpenCV 功能描述 静态图像人脸检测 视频人脸 ...

  6. UVa230 Borrowers

    原题链接 UVa230 思路 这题输入时有一些字符串处理操作,可以利用string的substr()函数和find_last_of()函数更加方便,处理时不必更要把书名和作者对应下来,注意到原题书名的 ...

  7. Codeforces475D - CGCDSSQ

    Portal Description 给出长度为\(n(n\leq10^5)\)的序列\(\{a_n\}\),给出\(q(q\leq3\times10^5)\)个\(x\),对于每个\(x\),求满足 ...

  8. Pymongo一些常见需求(陆续补充)

    总结一下最近包括之前遇到的一些pymongo操作的问题. #需求1: 搜索文档数组里边是否存在某元素 数据: data1 = { '_id': xxxxxxxxxxxxxx, 'dataList': ...

  9. CAN总线知识总结

    CAN总线知识整理 一.特点 二.CAN物理层 隐性(逻辑1),显性(逻辑0). 三.CAN数据链路层 3.1通信机制 3.2数据帧 3.3错误帧 3.4其它帧格式 3.5位定时与同步

  10. linux下stricky

    阅读文章链接http://www.linuxdiyf.com/viewarticle.php?id=79380   suid.sgid很易懂,stricky常忘了含义,做个笔记:   该位只对目录配置 ...