首先启动一个springboot的项目:

配置pow.xml,在maven里面添加依赖

<!--springboot之swaager的配置 start--><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>
<!--springboot之swaager的配置 end-->

编写swagger2的配置类:SwaggerConfig

package com.swaager.swaagerdemo.utils;/*** @authorseerhuitao 配置信息类* @create2019/6/13*/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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;/*** @author shengwu ni*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {@Beanpublic Docket createRestApi() {return new Docket(DocumentationType.SWAGGER_2)// 指定构建api文档的详细信息的方法:apiInfo().apiInfo(apiInfo()).select()// 指定要生成api接口的包路径,这里把controller作为包路径,生成controller中的所有接口.apis(RequestHandlerSelectors.basePackage("com.swaager.swaagerdemo.Cotroller")).paths(PathSelectors.any()).build();}/*** 构建api文档的详细信息* @return*/private ApiInfo apiInfo() {return new ApiInfoBuilder()// 设置页面标题.title("Spring Boot集成Swagger2接口总览")// 设置接口描述.description("数据挖掘springboot配置swaager")// 设置联系方式.contact("恵涛:" + "https://blog.csdn.net/chehec2010")// 设置版本.version("1.0")// 构建.build();}
}

启动springboot查看一下效果:输入地址

localhost:8080/swagger-ui.html

下一步编写实体类:User

package com.swaager.swaagerdemo.model;import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;/*** @authorseerhuitao 影虎描述* @create2019/6/13*/
@ApiModel(value = "用户实体类")
public class User {@ApiModelProperty(value = "用户唯一标识")private int id;@ApiModelProperty(value = "用户的名字")private String name;@ApiModelProperty(value = "用户的密码")private String passWord;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassWord() {return passWord;}public void setPassWord(String passWord) {this.passWord = passWord;}
}

编写指定的Cotroller类:

package com.swaager.swaagerdemo.Cotroller;import com.swaager.swaagerdemo.model.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** @authorseerhuitao 接口控制类* @create2019/6/13*/
@RestController
@RequestMapping("/user")
@Api(value = "swagger的在线文档")
public class SwCotroller {@GetMapping("/get/{id}")@ApiOperation(value = "根据用户唯一标识获取用户信息")public User getInfor(@PathVariable @ApiParam(value = "用户唯一标识")int id){User user = new User();user.setId(id);user.setName("huitao");user.setPassWord("123456");return user;}
}

再次输入地址:http://localhost:8080/swagger-ui.html

总结说明:

  • @ApiModel 注解用于实体类,表示对类进行说明,用于参数用实体类接收。
  • @ApiModelProperty 注解用于类中属性,表示对 Model 属性的说明或者数据操作更改。
  • @Api 注解用于类上,表示标识这个类是 Swagger 的资源。
  • @ApiOperation 注解用于方法,表示一个 HTTP 请求的操作。
  • @ApiParam 注解用于参数上,用来标明参数信息。

springboot细节挖掘(配置Swagger2)相关推荐

  1. springboot细节挖掘(集成ElasticSearch)

    ElasticSearch下载 官网地址:https://www.elastic.co/cn/downloads/elasticsearch DEMO参考地址:https://github.com/s ...

  2. springboot细节挖掘(jar和war打包)

    打包运行 springboot下面分别构建俩种包的构建演示 war包配置: <packaging>war</packaging> <build> <final ...

  3. springboot细节挖掘(日志系统)

    注意: 1.Spring Boot 1.3.x和以下版本支持log4j的日志输出 2.Spring Boot 1.3.x以上版本只支持log4j2,logback的日志输出 Slf4j+logback ...

  4. springboot细节挖掘(对测试的支持)

    Spring Boot 提供了专门支持测试的组件 Spring Boot Test,其集成了业内流行的 7 种强大的测试框架: JUnit,一个 Java 语言的单元测试框架: Spring Test ...

  5. springboot细节挖掘(监听器)

    什么是 Web 监听器?Web 监听器是一种 Servlet 特殊类,它们能帮助开发者监听 Web 中特定的事件,比如 ServletContext.HttpSession.ServletReques ...

  6. springboot细节挖掘(知识积累)

    转载地址:https://blog.csdn.net/HQZ820844012/article/details/80400058#spring-顶级框架

  7. Springboot细节挖掘(对web的支持之数据校验)

    数据校验: 输入验证是最重要的 Web 开发任务之一,在 Spring MVC 中有两种方式可以验证输入:一种是 Spring 自带的验证框架,另外一种是利用 JSR 实现. JSR 是一个规范文档, ...

  8. springboot细节挖掘(数据初始化)

    如何加载一些启动就需要的初始化数据呢? CommandLineRunner spring Boot 为我们提供了一个方法,通过实现接口 CommandLineRunner 来实现 定义初始化类 MyC ...

  9. JAVA入门[23]-SpringBoot配置Swagger2

    一.新建SpringBoot站点 1.新建module,然后引入pom依赖: <parent><groupId>org.springframework.boot</gro ...

最新文章

  1. syslinux引导GRUB4DOS
  2. vue router html后缀,vue-router.html
  3. WCF服务编程 学习笔记(2)
  4. aws sqs_在Spring使用AWS SQS创建消息驱动Bean
  5. java 并发 变量_实例讲解Java并发编程之变量
  6. linux 怎么管理文件夹,Linux 是如何管理目录文件?
  7. Atitit 软件开发中的艾提拉思想与理念总结 后端优先 手机优先 做好政治动员 高层抽象 一定要出理论结果书籍总结 技术就是艺术 三个软件层次的划分 实现层 规划层 艺术层 无限生
  8. Win10桌面背景(壁纸)导出工具
  9. linux下sd分区扩容,实用技巧:Linux系统分区容量扩充的方法
  10. a113 智能音箱芯片方案_智能音箱九大芯片方案商及其生产厂商和代表作品介绍...
  11. Kafka 入门二 kafka的安装启动
  12. 中级财务管理机考计算机,2017年中级会计师考试无纸化机考技巧
  13. 30天自制操作系统——第二十四天增加命令行窗口
  14. 双稳态电路的两个稳定状态是什么_单稳态电路与双稳态电路
  15. 关键路径法与关键链法区别
  16. 郝斌c语言大纲百度云,C语言学习大纲 郝斌(讲解)
  17. python中的几个容器--入门--小总结
  18. HTML+CSS+JavaScript 实现登录注册页面(超炫酷)
  19. APPStore版本更新时,App 预览和屏幕快照规则以及经验。
  20. 【鸿蒙OS开发入门】06 - 启动流程代码分析之KernelOS:之启动Linux-4.19 Kernel内核 启动init进程

热门文章

  1. bzoj 1070: [SCOI2007]修车【最小费用最大流】
  2. 用border做三角形
  3. sql server 2008新特性:资源调控器
  4. 做折线图_Excel折线图这样做,老板看了要崩溃
  5. GY39传感器C语言代码,详解Arduino GY-30数字光强传感器应用
  6. python 接口自动化测试_Python-基于数据驱动的接口自动化测试
  7. 三层调用关系_你真正的了解MVC三层架构开发模式吗
  8. APP设计灵感|高颜值时钟页面!让每一秒都过得有意义
  9. 壁纸控的你需要看这里!
  10. 设计灵感|如何让你的海报更具现代感?