Swagger是一款RESTFUL接口的文档在线自动生成+功能测试功能软件。

1、pom.xml添加配置

可以到http://mvnrepository.com上搜索springfox,便可以看到Springfox Swagger2和Springfox Swagger Ui的版本。

    <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>

2、swagger的configuration

package com.example.demo;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import io.swagger.annotations.ApiOperation;
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 SwaggerConfig { @Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//.apis(RequestHandlerSelectors.basePackage("com.example.demo.web"))//包所在位置
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any()).build().useDefaultResponseMessages(false);
} /**
* 获取swagger ApiInfo
*
* @return
*/
private static ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("API文档")
.description("Swagger API 文档")
.termsOfServiceUrl("http://www.cnblogs.com/gdjlc")
.version("1.0")
.contact(new Contact("name..", "url..", "email.."))
.build();
}
}

3、Controller中测试方法

package com.example.demo.web;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation; @RestController
@Api(value = "UserController", tags = "用户操作相关接口")
public class UserController { @ResponseBody
@GetMapping("/helloWorld")
@ApiOperation(value="测试value", notes = "测试notes")
@ApiImplicitParam(paramType="query", name = "userName", value = "用户编号", required = true, dataType = "String")
public String helloWorld(@RequestParam String userName){
return "hello world," + userName;
}
}

Swagger使用的注解及其说明:
@Api:用在类上,说明该类的作用。
@ApiOperation:注解来给API增加方法说明。
@ApiImplicitParams : 用在方法上包含一组参数说明。
@ApiImplicitParam:用来注解来给方法入参增加说明。
@ApiResponses:用于表示一组响应
@ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息
@ApiModel:描述一个Model的信息

访问:项目地址/swagger-ui.html,如我的http://localhost:9001/swagger-ui.html
结果如下,可以点击方法右边的【Try it out】按钮开始测试,之后输入参数,点击【Execute】执行查看结果。

Spring Boot 2 整合Swagger简单入门的更多相关文章

  1. Spring Boot:整合Swagger文档

    综合概述 spring-boot作为当前最为流行的Java web开发脚手架,越来越多的开发者选择用其来构建企业级的RESTFul API接口.这些接口不但会服务于传统的web端(b/s),也会服务于 ...

  2. Spring Boot 快速整合Swagger

    一.前言 Spring Boot作为当前最为流行的Java web开发脚手架,越来越多的开发者选择用其来构建企业级的RESTFul API接口.这些接口不但会服务于传统的web端(b/s),也会服务于 ...

  3. Spring Boot:整合Swagger

    1.先创建一个SpringBoot项目 其中application.properties文件中是创建项目时自动添加的配置. 2.添加相关maven依赖 <!--swagger--> < ...

  4. Spring Boot:整合Spring Security

    综合概述 Spring Security 是 Spring 社区的一个顶级项目,也是 Spring Boot 官方推荐使用的安全框架.除了常规的认证(Authentication)和授权(Author ...

  5. Spring Boot:整合MyBatis框架

    综合概述 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以使用简单 ...

  6. Spring Boot:整合Spring Data JPA

    综合概述 JPA是Java Persistence API的简称,是一套Sun官方提出的Java持久化规范.其设计目标主要是为了简化现有的持久化开发工作和整合ORM技术,它为Java开发人员提供了一种 ...

  7. Spring Boot:整合Shiro权限框架

    综合概述 Shiro是Apache旗下的一个开源项目,它是一个非常易用的安全框架,提供了包括认证.授权.加密.会话管理等功能,与Spring Security一样属基于权限的安全框架,但是与Sprin ...

  8. Spring Boot:整合JdbcTemplate

    综合概述 Spring对数据库的操作在jdbc上面做了更深层次的封装,而JdbcTemplate便是Spring提供的一个操作数据库的便捷工具.我们可以借助JdbcTemplate来执行所有数据库操作 ...

  9. Spring Boot 2.0 的快速入门(图文教程)

    摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! Spring Boot 2.0 的快速入门(图文教程) 大家都 ...

随机推荐

  1. Mock Server 实现post方法的接口(三)

    Mock Server 实现post方法的接口(三) 1.mock server实现的接口,当request中未设置"method"时,会自动将所有method试一次,所以一定要指 ...

  2. 微信小程序登录流程及解析用户openid session_key,获取用户信息

    为优化用户体验,使用 wx.getUserInfo 接口直接弹出授权框的开发方式将逐步不再支持.从2018年4月30日开始,小程序与小游戏的体验版.开发版调用 wx.getUserInfo 接口,将无 ...

  3. centos下etcd集群安装

    先仔细了解学习etcd 官方: https://github.com/etcd-io/etcd https://www.cnblogs.com/softidea/p/6517959.html http ...

  4. pc端 页面 显示在手机 一行控制适配问题

    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">

  5. UIView的绘制原理

    当UIView调用setNeedDisplay之后, 系统会调用view对应layer的 setNeedsDisplay, 在当前runloop即将结束的时候调用CALayer的display方法. ...

  6. 数据库主库从库宕机重启后binlog数据同步

    由于阿里云经典网络迁移到专用网络,一不小心没有先预备方案调整网段, 导致实例无法以内网IP形式访问数据库,被迫进行数据库停机后网络网段调整,导致宕机了几个小时...被客户各种投诉爆了.. 基于这次数据 ...

  7. T-SQL语言基础(2)之SQL Server体系结构

    SQL Server 体系结构 SQL Server 实例 SQL Server 实例是指安装的一个 SQL Server 数据库引擎/服务.在同一台计算机上可以安装 SQL Server 的多个实例 ...

  8. ln -s软链接文件算文件吗

    场景: 开发A在windows环境下完成了开发,配置管理员cm搭建jenkins在centos环境下编译,cm编译失败,但是开发A在他的windows环境下可以编译过,最后发现是某几个so文件的软链接 ...

  9. [LeetCode] 97. Interleaving String_ Hard tag: Dynamic Programming

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Example 1: Input: s1 = ...

  10. Android 浏览器内 H5 电脑 Chrome 调试

    Android 浏览器内 H5 调试 chrome://inspect 移动前端调试方案(Android + Chrome 实现远程调试) adb 相关资源 adb shell(ADB Kits)下载 ...