swagger是一个功能强大的api框架,它的集成非常简单,不仅提供了在线文档的查阅,而且还提供了在线文档的测试。

(1) 引入依赖,我们选择现在最新的版本

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

(2) 写配置类

/**
* @Auther: curry
* @Date: 2018/6/3 12:46
* @Description:
*/
@Configuration
@EnableSwagger2
public class SwaggerProperties {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.imooc.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("springboot利用swagger构建api文档")
.description("")
.termsOfServiceUrl("")
.version("1.0")
.build();
}
}

(3) 在controller 中进行引入注解(当然也可以不写)


/**
* @Auther: curry
* @Date: 2018/5/28 21:57
* @Description:
*/
@RestController
public class GirlController {
private final static Logger logger = LoggerFactory.getLogger(GirlController.class);
@Resource
private GirlRepository girlRepository; @Resource
private GirlService girlService; @ApiOperation(value="获取女孩列表", notes="获取女孩列表")
@GetMapping("/girls")
public List<Girl> getList(){
logger.info("getList");
return girlRepository.findAll();
} @ApiOperation(value = "添加女孩" ,notes="添加女孩")
@PostMapping("/girls")
public Result<Girl> girlAdd(@Valid Girl girl, BindingResult bindingResult){
if(bindingResult.hasErrors()){
return ResultUtil.error(1,bindingResult.getFieldError().getDefaultMessage());
}
return ResultUtil.success(girlRepository.save(girl)); } @ApiOperation(value = "查找女孩",notes = "查找女孩")
@ApiImplicitParam(name = "id" ,value ="查找女孩" ,required = true,dataType = "int",paramType = "path")
@GetMapping(value = "/girls/{id}")
public Girl find(@PathVariable(value = "id") Integer id){
return girlRepository.findById(id).get();
} @ApiOperation(value = "修改女孩",notes = "根据id查找女孩并修改")
@PostMapping(value = "/girls/{id}")
public Girl update(@PathVariable(value = "id") Integer id,
@RequestParam("name") String name,
@RequestParam("age") int age){
Girl girl = new Girl();
girl.setId(id);
girl.setAge(age);
girl.setName(name);
return girlRepository.save(girl); } @ApiOperation(value = "删除女孩",notes = "根据id删除女孩")
@DeleteMapping(value = "/girls/{id}")
public void delete(@PathVariable(value = "id") Integer id){
girlRepository.deleteById(id);
} @ApiOperation(value = "根据年龄查询女孩")
@GetMapping(value = "/girls/age/{age}")
public List<Girl> findByAge(@PathVariable(value = "age") Integer age){
return girlRepository.findByAge(age);
} @GetMapping(value = "/girls/getAge/{id}")
public void getAge(@PathVariable("id") Integer id) throws Exception {
girlService.getAge(id);
}
}

(4) 访问http://localhost:8099/swagger-ui.html



(5)当然,你也可以不在controller 中增加这个注解,是不是感觉很方便,很能测试,just do it!

SpringBoot(八)_springboot集成swagger2的更多相关文章

  1. SpringBoot(十)_springboot集成Redis

    Redis 介绍 Redis是一款开源的使用ANSI C语言编写.遵守BSD协议.支持网络.可基于内存也可持久化的日志型.Key-Value高性能数据库. 数据模型 Redis 数据模型不仅与关系数据 ...

  2. SpringBoot(九)_springboot集成 MyBatis

    MyBatis 是一款标准的 ORM 框架,被广泛的应用于各企业开发中.具体细节这里就不在叙述,大家自行查找资料进行学习下. 加载依赖 <dependency> <groupId&g ...

  3. SpringBoot集成Swagger2在线文档

    目录 SpringBoot集成Swagger2在线文档 前言 集成SpringBoot 登录接口文档示例 代码 效果 注解说明 总结 SpringBoot集成Swagger2在线文档 前言 不得不说, ...

  4. 【java框架】SpringBoot(3) -- SpringBoot集成Swagger2

    1.SpringBoot web项目集成Swagger2 1.1.认识Swagger2 Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体 ...

  5. SpringBoot集成Swagger2实现Restful(类型转换错误解决办法)

    1.pom.xml增加依赖包 <dependency> <groupId>io.springfox</groupId> <artifactId>spri ...

  6. springboot集成swagger2构建RESTful API文档

    在开发过程中,有时候我们需要不停的测试接口,自测,或者交由测试测试接口,我们需要构建一个文档,都是单独写,太麻烦了,现在使用springboot集成swagger2来构建RESTful API文档,可 ...

  7. springboot 集成swagger2

    使用Swagger 可以动态生成Api接口文档,在项目开发过程中可以帮助前端开发同事减少和后端同事的沟通成本,而是直接参照生成的API接口文档进行开发,提高了开发效率.这里以springboot(版本 ...

  8. springboot 集成swagger2.x 后静态资源报404

    package com.bgs360.configuration; import org.springframework.context.EnvironmentAware; import org.sp ...

  9. SpringBoot 集成Swagger2自动生成文档和导出成静态文件

    目录 1. 简介 2. 集成Swagger2 2.1 导入Swagger库 2.2 配置Swagger基本信息 2.3 使用Swagger注解 2.4 文档效果图 3. 常用注解介绍 4. Swagg ...

随机推荐

  1. windows10如何将python2和python3添加到环境变量中

    点击我的电脑----->右键‘属性’----->高级系统管理-------->高级-------->环境变量------>新建------->此时输入变量名和变量值 ...

  2. 日常的例子说明 throttle 和 debounce 的区别

    不小心接触到 throttle 和 debounce,按捺不住猎奇的心理,找这两个函数的资料. 然而百度到的各种对他们的理解,我去啊. 艰难地搞明白他们是干嘛的之后,忍不住举个例子说说自己的理解,希望 ...

  3. 几个原生js方法总结

    一.document.getElementById('emoji').addEventListener('click', function(e) { var emojiwrapper = docume ...

  4. VSCode中C/C++库文件的配置

    VSCode中C/C++库文件的配置 之前一直在是用sublime做主要编辑器,现在主要使用VSCode,毕竟大厂制作,从目前的使用情况来看,我更喜欢使用VSCode编辑器. 有时候会用VScode来 ...

  5. VMware Workstation and Device/Credential Guard are not compatible

    VMware Workstation and Device/Credential Guard are not compatible. VMware Workstation can be run aft ...

  6. C++ chrono 库中的 steady_clock 和 system_clock

    C++11 中提供了一个计时的标准库 <chrono>; 里面有三种时钟 clock: steady_clock, system_clock 和 high_resolution_clock ...

  7. 学习python,第四篇:Python 3中bytes/string的区别

    原文:http://eli.thegreenplace.net/2012/01/30/the-bytesstr-dichotomy-in-python-3 python 3中最重要的新特性可能就是将文 ...

  8. 从零开始的Python学习Episode 20——面向对象(3)

    面向对象之封装 封装,即隐藏对象的属性和实现细节,仅对外公开接口,控制在程序中属性的读和修改的访问级别:将抽象得到的数据和行为(或功能)相结合,形成一个有机的整体. 隐藏 在python中用双下划线开 ...

  9. python3 通过qq邮箱定时发送邮件

    下面的代码为了每天定时发送监控邮件,监控什么呢?监控当天redis队列中是否有没有消费的数据,和当天mysql中新增的数据量 # -*- coding:utf-8 -*- from common.re ...

  10. shell--read命令

    read命令 -p(提示语句) -n(字符个数) -t(等待时间) -s(不回显) 1.基本读取read命令接收标准输入(键盘)的输入,或其他文件描述符的输入(后面在说).得到输入后,read命令将数 ...