1、在项目pom文件中引入swagger2的jar包

<!-- swagger2开始 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.4.0</version>
</dependency>
<!-- swagger2结束 -->

2、编写用于配置swagger2的类  Swagger2.java(名字可任意取)

package com.example.demo;

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.example.demo"))
.paths(PathSelectors.any())
.build();
}
//构建 api文档的详细信息函数,注意这里的注解引用的是哪个
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
//页面标题
.title("SpringBoot的Swagger2构建RESTful API接口形式")
//创建人
.contact(new Contact("cxy", "https://www.cnblogs.com/lazycxy/", "cxymasters@163.com"))
//版本号
.version("1.0")
//描述
.description("API相关描述")
.build();
}
}

3、在你的接口类中添加如下注释

@RestController
@Api("测试接口描述")
public class HelloWorld {
@RequestMapping("/hello")
@ApiOperation(value = "方法介绍描述", httpMethod = "GET", response = String.class, notes = "方法介绍描述")
public String hello(
@RequestParam(value = "field", required = false)@ApiParam("参数描述") String field
){
return "hello world~~~";
}
}

4、启动你的项目,输入localhost:8081/swagger-ui.html  进行访问swagger页面  ps:这个swagger-ui.html 是封装到swagger的jar包里的

整合完毕

最新文章

  1. 优化ABAP性能(摘录)
  2. IE兼容模式下两个小问题,JSON.stringify和SCRIPT70 无权限
  3. hadoop-初学者写map-reduce程序中容易出现的问题 3
  4. Mysql视图的作用及其性能分析
  5. Hadoop中Combiner的作用
  6. The test form is only available for requests from the local machine 解决方法
  7. js获取url中的参数对象、js生成带参数的url
  8. Xcode5和6上新建工程如何本地化启动页面
  9. ABCpdf.NET中Rect,Bottom,Height的关系
  10. hibernate框架学习笔记12:查询优化
  11. 夜神模拟器调试web APP
  12. 排序之选择排序(SelectSort)
  13. python 列表 元祖 集合
  14. 如何查看linux版本信息
  15. Java编码常见的Log日志打印问题
  16. PHPCMS9.6.0最新版SQL注入和前台GETSHELL漏洞分析 (实验新课)
  17. LR报错Error -27780: [GENERAL_MSG_CAT_SSL_ERROR]connect to host &quot;XXX.XXX.com&quot; failed解决方法
  18. Kotlin 范型约束
  19. docker(三)反正我不喜欢敲命令,daocloud.io管理你的docker,安装远程下载工具aria2 迅雷远程下载 xware
  20. mysql5.6修改字符编码,ERR:Illegal mix of collations for operation &#39;concat&#39;

热门文章

  1. 自定义View之开关
  2. [sublime3] 在linux下的终端中使用sublime3打开文件
  3. httpclient 方式提供接口
  4. zabbix 支持的主要监控方式
  5. JavaScript Boolean(逻辑)对象
  6. 一道看似简单的go程序的深入分析
  7. JAVA并发之阻塞队列浅析
  8. 小伙子,你真的清楚 JVM GC ?
  9. SmartSql使用教程(4)——多库配置与使用
  10. 一文搞懂Python迭代器和生成器