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

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

一、创建一个SpringBoot的maven项目

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

项目创建后的目录;

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

       <dependency>

           <groupId>io.springfox</groupId>

           <artifactId>springfox-swagger2</artifactId>

           <version>2.8.0</version>

         </dependency>

       <dependency>

           <groupId>io.springfox</groupId>

           <artifactId>springfox-swagger-ui</artifactId>

           <version>2.8.0</version>

       </dependency>

直接使用appache的仓库:

         <repository>
             <id>springfox-swagger</id>
             <url>https://mvnrepository.com/artifact/io.springfox/springfox-swagger2</url>
         </repository>
         <repository>
             <id>springfox-swagger-ui</id>
             <url>https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui</url>
         </repository>

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

 package com.elon.springbootdemo.config;

 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

 import springfox.documentation.builders.ApiInfoBuilder;
 import springfox.documentation.builders.PathSelectors;
 import springfox.documentation.builders.RequestHandlerSelectors;
 import springfox.documentation.service.ApiInfo;
 import springfox.documentation.spi.DocumentationType;
 import springfox.documentation.spring.web.plugins.Docket;
 import springfox.documentation.swagger2.annotations.EnableSwagger2;

 @Configuration
 @EnableSwagger2
 public class Swagger2Config extends WebMvcConfigurerAdapter {
     @Bean
     public Docket api() {
         return new Docket(DocumentationType.SWAGGER_2)
                 .select()
                 .apis(RequestHandlerSelectors.basePackage("com.elon.springbootdemo.ws"))
                 .paths(PathSelectors.any())
                 .build()
                 .apiInfo(getApiInfo());
     }

     private ApiInfo getApiInfo()
     {
         ApiInfo apiInfo =  new ApiInfoBuilder().title("用户管理模块")
                 .description("定义用户数据的增加、删除、修改接口")
                 .termsOfServiceUrl("http://www.cnblogs.com/elon")
                 .version("1.0")
                 .build();
         return apiInfo;
     }
 }

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

 package com.elon.springbootdemo.ws;

 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestHeader;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;

 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;

 @RestController
 @RequestMapping(value="swagger-demo")
 @Api(value="WSUserSwagger", description="用户信息管理")
 public class WSUserSwagger {

     @ApiOperation(value="添加用户", notes="添加用户")
     @RequestMapping(value="/v1/user", method=RequestMethod.POST)
     public String addUser(@RequestBody String userInfo) {
         return "Add user:" + userInfo;
     }

     @ApiOperation(value = "根据名称查询用户", notes = "根据名称查询用户")
     @RequestMapping(value = "/v1/user", method = RequestMethod.GET)
     public String queryUserByName(@RequestParam("name") String name, @RequestHeader("age") int age) {
         return name + age;
     }

     @ApiOperation(value="删除用户", notes="删除用户")
     @RequestMapping(value="/v1/user/{name}", method=RequestMethod.DELETE)
     public String deleteUser(@PathVariable("name") String name) {
         return "delete " + name;
     }
 } 

五、启动后测试

在浏览器中输入 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. ElasticSearch Kibana 和Logstash 安装x-pack记录

    前言 最近用到了ELK的集群,想想还是用使用官方的x-pack的monitor功能对其进行监控,这里先上图看看: 环境如下: 操作系统: window 2012 R2 ELK : elasticsea ...

  2. ubuntu下安装memcached与php扩展测试使用

    1,memcached需要libevent,所以要先安装它 下载地址:http://download.chinaunix.net/download.php?id=45065&ResourceI ...

  3. os模块中关于文件/目录常用的函数使用方法

    os模块中关于文件/目录常用的函数使用方法 函数名 使用方法 getcwd() 返回当前工作目录 chdir(path) 改变工作目录 listdir(path='.') 列举指定目录中的文件名('. ...

  4. Servlet3.0+springmvc5+log4j2正确的开启姿势(WebLookUp)

    前言 java社区占据市场份额比较大的日志组件由log4j 1.×,到logback,再到整合后的升级版 log4j 2.×,有网友测试后据说log4j2的性能最NB.于是开始往自己的springmv ...

  5. J2EE和android的GZIP测试

    使用GZIP进行数据压缩传输实验,服务端是J2EE,使用HTTP的POST方式进行数据请求. 为了方便测试,刚开始在J2EE的环境下写了一个TestCase去调用J2EE写的服务,忘记写GZIP解压代 ...

  6. Java集合框架(六)—— Collections工具类

    操作集合的工具类Collections Java提供了一个操作Set.List和Map等集合的工具类:Collections,该工具类里提供了大量方法对集合元素进行排序.查询和修改等操作,还提供了将集 ...

  7. python file文件操作--内置对象open

    说明: 1. 函数功能打开一个文件,返回一个文件读写对象,然后可以对文件进行相应读写操作. 2. file参数表示的需要打开文件的相对路径(当前工作目录)或者一个绝对路径,当传入路径不存在此文件会报错 ...

  8. R︱shiny实现交互式界面布置与搭建(案例讲解+学习笔记)

    要学的东西太多,无笔记不能学~~ 欢迎关注公众号,一起分享学习笔记,记录每一颗"贝壳"~ --------------------------- 看了看往期的博客,这个话题竟然是第 ...

  9. 错误:readline/readline.h:没有那个文件或目录解决方法

    curl -R -O http://www.lua.org/ftp/lua-5.3.0.tar.gz tar zxf lua-5.3.0.tar.gz cd lua-5.3.0 make linux ...

  10. CSS3之box-shadow

    1.属性简介 box-shadow:颜色值|inset|none|!important 2.浏览器兼容性 (1)IE不兼容,IE9和IE10未知: (2)火狐3.5(包含3.5)以上兼容 (3)Chr ...