前言

Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,参数和模型紧密集成到服务器端的代码,允许API来始终保持同步。Swagger 让部署管理和使用功能强大的API从未如此简单。

一、修改pom.xml文件,加入依赖

           <!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>

二、添加Swagger配置类

package com.slp.util;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2; import static springfox.documentation.builders.PathSelectors.regex; /**
* Created by sangliping on 2017/8/17.
*/
@Configuration
@EnableSwagger2
public class Swaggers { @Bean
public Docket swaggerSpringMvcPlugin() {
ApiInfo apiInfo = new ApiInfo("sample of springboot", "sample of springboot", null, null, null, null, null);
Docket docket = new Docket(DocumentationType.SWAGGER_2).select().paths(regex("/user/.*")).build()
.apiInfo(apiInfo).useDefaultResponseMessages(false);
return docket;
} /*private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("测试API")
.description("测试API1")
.version("1.0.0")
.build();
}*/
/* @Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.slp.web"))
.paths(regex("/user/.*"))
.build();
}
*/ }

三、编写测试用的Controller类

package com.slp.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
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 sangliping on 2017/8/17.
* Swagger常用注解:
* @Api:修饰整个类,描述Controller的作用
* @ApiOperation:描述一个类的一个方法,或者说一个接口
* @ApiParam:单个参数描述
* @ApiModel:用对象来接收参数
* @ApiProperty:用对象接收常熟市,描述对象的一个字段
* @ApiIgnore:使用该注解时忽略这个API
* @ApiResponse:HTTP响应其中一个描述
* @ApiResponses:HTTP响应整个描述
*/
@RestController
@RequestMapping("/user")
@Api(value = "Shop")
public class SpringBootController {
@ApiOperation(value = "获取helloworld",notes = "简单的Spring boot请求")
@RequestMapping
String home (){
return "HELLO WORLD";
} @ApiOperation(value = "获得参数",notes = "根据参数中的classNo和studentName是的字符串相加")
@ApiImplicitParams({@ApiImplicitParam(name = "classNo",value ="班级编号",required= true,dataType="String")})
@RequestMapping(value="/class/{classNo}/to/{studentName}",method = RequestMethod.GET)
String world(@PathVariable("classNo")String classNo,@PathVariable("studentName")String studentName){
return classNo + " "+studentName;
} }

四、Swagger启动

【Spring Boot&&Spring Cloud系列】Spring Boot项目集成Swagger UI的更多相关文章

  1. 15、Spring Boot 2.x 集成 Swagger UI

    1.15.Spring Boot 2.x 集成 Swagger UI 完整源码: Spring-Boot-Demos 1.15.1 pom文件添加swagger包 <swagger2.versi ...

  2. 项目集成swagger,并暴露指定端点给swagger

    项目集成swagger 一:思考: 1.swagger解决了我们什么问题? 传统开发中,我们在开发完成一个接口后,为了测试我们的接口,我们通常会编写单元测试,以测试我们的接口的可用性,或者用postm ...

  3. Maven + SpringMVC项目集成Swagger

    Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体目标是使客户端和文件系统作为服务器以同样的速度来更新.文件的方法,参数和模型紧密集成到服 ...

  4. ASP.NET Core 2.2 WebApi 系列【四】集成Swagger

    Swagger 是一款自动生成在线接口文档+功能测试功能软件 一.安装程序包 通过管理 NuGet 程序包安装,搜索Swashbuckle.AspNetCore 二.配置 Swagger 将 Swag ...

  5. MVC项目集成swagger

    1.创建WebAPI项目解决方案 2.使用nuget引入Swashbuckle包 引入Swashbuckle包后App_Start文件夹下会多出一个SwaggerConfig文件 3.添加接口注释 项 ...

  6. spring boot 2.x 系列 —— spring boot 实现分布式 session

    文章目录 一.项目结构 二.分布式session的配置 2.1 引入依赖 2.2 Redis配置 2.3 启动类上添加@EnableRedisHttpSession 注解开启 spring-sessi ...

  7. spring boot 2.x 系列 —— spring boot 整合 redis

    文章目录 一.说明 1.1 项目结构 1.2 项目主要依赖 二.整合 Redis 2.1 在application.yml 中配置redis数据源 2.2 封装redis基本操作 2.3 redisT ...

  8. spring boot 2.x 系列 —— spring boot 整合 dubbo

    文章目录 一. 项目结构说明 二.关键依赖 三.公共模块(boot-dubbo-common) 四. 服务提供者(boot-dubbo-provider) 4.1 提供方配置 4.2 使用注解@Ser ...

  9. spring boot 2.x 系列 —— spring boot 整合 druid+mybatis

    源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all 一.说明 1.1 项目结构 项目查询用的表对应的建表语句放置在resour ...

随机推荐

  1. ISAPI和CGI限制中没有ASP.NET v4.0 ; vS2013检测到在集成的托管管道模式下不适用的 ASP.NET 设置。

    统确实自带了ASP.NET v4.0,但是ISAPI中没有这个选项,导致服务器开不起来 解决方法如下: 1.确保安装IIS时确实安装了ASP.NET,如果没有的话,勾上重新装一下,一般出现404.2时 ...

  2. RMAN命令总结

    一,RMANR 连接到库 1),连接本地数据库 a,如果本地有多少实例,则需要设置环境变量ORACLE_SID,  windows 平台: set ORACLE_SID= INSTANACE NAME ...

  3. Centos7 安装redis服务

    Redis的安装 1.先安装gcc编译器,否则make的时候会报错 yum -y install gcc 2.下载redis安装包,解压编译安装 $ wget http://download.redi ...

  4. js 历史

    原文http://javascript.ruanyifeng.com/introduction/history.html JavaScript的诞生 JavaScript 因为互联网而生,紧随着浏览器 ...

  5. winform c#中子窗体关闭刷新父窗体

    父窗体Form1 子窗体Form2 Form1中有一个datagridview控件和一添加按钮,Form2中有一个Text控件和一个保存按钮 要求点击Form1窗体上的添加按钮,弹出Form2,再te ...

  6. 如何获取模拟器安装的app的位置

    你可以死记下地址格式, 但是一旦不同的xcode和模拟器版本改变变了地址, 又得记, 从活动管理器里其实是可以直接查看的: Launch the app in the simulator Open A ...

  7. redis 的 HyperLogLog

    Redis 在 2.8.9 版本添加了 HyperLogLog 结构. Redis HyperLogLog 是用来做基数统计的算法 HyperLogLog 的优点是,在输入元素的数量或者体积非常非常大 ...

  8. git 强制刷新,放弃更改

    git fetch --all  git reset --hard origin/master

  9. Phpcms V9手机门户设置教程:怎么用PC V9做手机网站

    一.在PHPcms V9管理后台设置手机门户 1.1.开启手机网站.位置:模块 >手机门户 > 添加手机站点,具体设置可参照截图: 填写站点名和LOGO文件相对位置,绑定用于手机网站的二级 ...

  10. 一致性Hash算法原理及C#代码实现

    一.一致性Hash算法原理 基本概念 一致性哈希将整个哈希值空间组织成一个虚拟的圆环,如假设某哈希函数H的值空间为0-2^32-1(即哈希值是一个32位无符号整形),整个哈希空间环如下: 整个空间按顺 ...