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. [HNOI2015]开店 树链剖分,主席树

    [HNOI2015]开店 LG传送门 蒟蒻表示不会动态淀粉质. 先把点按年龄排序, 设\(dis[i]\)表示\(i\)到根的距离. 把我们要算的东西稍微变下形:\(ans\) \[ = \sum \ ...

  2. AFO预定

    妈耶 数论题都不会 推不出式子 题解都看不懂 还是思维jiang化了 布星了 吃枣药丸 祝yyb进队 祝zsy进队 祝鸡贼进队

  3. 【linux报错】安装好虚拟机后,挂载光盘报错:mount:you must specify the filesystem type

    问题现象: 问题原因: 当时光盘的“已连接”的勾没有勾上 解决后:

  4. Docker和CI/CD实战

    一.CICD和DevOps 前面已经了解了CI/CD,其实CI/CD已经存在多年了,只是最近软件工程方面又提出了敏捷开发.DevOps,又把CI/CD炒火了. 那么什么是DevOps?DevOps和C ...

  5. Python之元类详解

    一.引子 元类属于Python面向对象编程的深层魔法,99%的人都不得要领,一些自以为搞明白元类的人其实也是自圆其说,点到为止,从队元类的控制上来看就破绽百出,逻辑混乱: 二.什么是元类 一切源自于一 ...

  6. oracle的多表合并查询-工作心得

    本随笔文章,由个人博客(鸟不拉屎)转移至博客园 发布时间: 2018 年 11 月 29 日 原地址:https://niaobulashi.com/archives/oracle-select-al ...

  7. 高可用OpenStack(Queen版)集群-5.Glance集群

    参考文档: Install-guide:https://docs.openstack.org/install-guide/ OpenStack High Availability Guide:http ...

  8. react + antiDesign开发中遇到的问题记录

    react + antiDesign开发中遇到的问题记录 一:页面中子路由失效: antiDesign的官方实例中,会把路由重复的地方给去重,而且路由匹配模式不是严格模式.所以我们需要在util.js ...

  9. 团队冲刺——Three

    第三天计划: 季方:学习爬虫的操作,以便后续功能实现: 司宇航:对当天实现的功能进行总的测试: 王金萱:数据库内数据的增删改查以及查看团队博客界面的实现: 马佳慧:学习css初步,进行页面绘制: 第二 ...

  10. Hibernate笔记②--hibernate类生成表、id生成策略、级联设置、继承映射

    一.多表的一个关联关系 老师和学生是一对多的关系 student:tid属性 外键约束 对应teacher表中的id属性 teacher:id 在myeclipse的db窗口中选中两个表来生成类.   ...