0-前言

  现在的项目开发,基本都是前后端分离,后端专注于API接口开发,都需要编写和维护API接口文档。如果你还在用Word来编写接口文档,那你就out了,这个时候,当当当当~神兵利器swagger隆重出场!Swagger根据项目自动生成API文档,可以帮助我们更好的编写和维护API文档。Spring boot集成swagger那是必须的,好不好用,谁用谁知道。

1-springboot 集成swagger

  swagger集成swagger很简单,只需几步即可:

1-1、添加maven依赖:

    <!-- 4、集成swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>

1-2、添加swagger配置

package anson.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
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; /**
* @description: Swagger配置类
* @author: anson
* @Date: 2019/9/3 22:52
* @version: 1.0
*/ @Configuration
@EnableSwagger2
public class SwaggerConfig
{
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//swagger要扫描的包路径
.apis(RequestHandlerSelectors.basePackage("anson.controller"))
.paths(PathSelectors.any())
.build();
} private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("springboot Swagger 测试")
.description("Springboot 整合Swagger2")
.termsOfServiceUrl("localhost:8090")
.contact(new Contact("Swagger测试","localhost:8090/swagger-ui.html","XXXX@qq.com"))
.version("1.0")
.build();
}
}

1-3、在controller中加入相关swagger注解

package anson.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; /**
* @description: user接口
* @author: anson
* @Date: 2019/9/3 21:33
* @version: 1.0
*/ @Api(value = "用户接口")
@RestController
@RequestMapping("/user")
public class User
{
/*
* @Author anson
* @Description 有参数的方法
* @Date 2019/9/3 23:29
* @Param [id]
* @return java.lang.String
*/
@ApiOperation(value = "获取用户", notes = "根据id查询用户信息")
@ApiImplicitParam(name = "id", value = "用户id", required=true, dataType="String") //API参数
@RequestMapping(value="/getUserById",method= RequestMethod.GET)
public String getUser(String id)
{
return ("Hello anson-" + id);
} /*
* @Author anson
* @Description 无参数的方法
* @Date 2019/9/3 23:29
* @Param []
* @return java.lang.String
*/
@ApiOperation(value = "获取用户", notes = "获取用户信息")
@RequestMapping(value="/getUser",method= RequestMethod.GET)
public String getUser()
{
return "Hello anson!";
}
}

完毕,就这么简单;运行行后输入配置的网址即可看见API文档,在上面可以直接运行API进行测试

源码地址:https://github.com/anson-yang/cloverDemo.git

好,本节结束,下节见

小白的springboot之路(二)、集成swagger的更多相关文章

  1. 小白的springboot之路(一)、环境搭建、第一个实例

    小白的springboot之路(一).环境搭建.第一个实例 0- 前言 Spring boot + spring cloud + vue 的微服务架构技术栈,那简直是爽得不要不要的,怎么爽法,自行度娘 ...

  2. 小白的springboot之路(十九)、集成swagger(com.spring4all篇)

    0-前言 集成swagger,有两种方式: 一种在前面已经介绍过了,直接集成官方的springfox-swagger2的方式,这种方式需要在配置类中配置 第二种方式是这里要介绍的方式,国人写的com. ...

  3. (九) SpringBoot起飞之路-整合/集成Swagger 2 And 3

    兴趣的朋友可以去了解一下其他几篇,你的赞就是对我最大的支持,感谢大家! (一) SpringBoot起飞之路-HelloWorld (二) SpringBoot起飞之路-入门原理分析 (三) Spri ...

  4. 小白的springboot之路(十二)、集成log4j2日志

    0.前言 日志记录对系统来说必不可少,spring boot中常用的日志组件有log4j.logback.log4j2,其中logback是spring boot默认的,已自带:选用log4j2就可以 ...

  5. 小白的springboot之路(三)、集成mybatis与MySQL

    0.前言 mybatis属于半自动的ORM,相比hibernate这种全自动的ORM,兼顾了性能与易用:目前企业项目中,基本都是mybatis的天下:今天就来整合mybatis与MySQL: 1.整合 ...

  6. 小白的springboot之路(九)、集成MongoDB

    0.前言 MongoDB是一个高性能.开源的文档型数据库,是当前nosql数据库中最热门的一种,在企业中广泛应用:虽然前段时间更改了开源协议导致被很多企业舍弃,但主要是对云服务商影响较大,对我们来说其 ...

  7. 小白的springboot之路(五)、集成druid

    0-前言 Druid阿里巴巴开源的一个java数据库连接池,是Java语言中最好的数据库连接池,Druid能够提供强大的监控和扩展功能:集成它能够方便我们对数据库连接进行监控和分析,下面我们来集成它: ...

  8. 小白的springboot之路(八)、继承Redis以及@Cacheable注解实现Redis缓存

    0.前言 在项目中,缓存作为一种高效的提升性能的手段,几乎必不可少,Redis作为其中的佼佼者被广泛应用: 一.spring boot集成Redis 1.添加依赖 <dependency> ...

  9. 小白的springboot之路(十一)、构建后台RESTfull API

    0.前言 开发系统中,前后端分离,后端一般返回RESTfull  API,前端调用API构建UI,彼此分离.互相完全独立: 后台API中,我们一般返回结果码.提示信息.数据三部分内容,如图: 我们今天 ...

随机推荐

  1. Bootstrap布局基础

     1.栅格系统(布局)Bootstrap内置了一套响应式.移动设备优先的流式栅格系统,随着屏幕设备或视口(viewport)尺寸的增加,系统会自动分为最多12列. 我在这里是把Bootstrap中的栅 ...

  2. 学习笔记45_log4net日志

    1.配置添加一个App.config *对于网站,就使用web.config ***对于App.config和web.config的配置,在表现形式上是不一致的,使用的时候应该在网上查对于的配置设置. ...

  3. CentOS 8 发布了

    CentOS 8 的发现注记是: https://wiki.centos.org/Manuals/ReleaseNotes/CentOS8.1905 CentOS 在 2019 年 9 月 25 日 ...

  4. NOIP模拟21+22

    模拟21确实毒瘤...考场上硬刚T3 2.5h,成功爆零 T1.数论 看这题目就让人不想做,考场上我比较明智的打完暴力就弃掉了,没有打很久的表然后找规律. 正解貌似是乱搞,我们考虑一个比较显然的结论: ...

  5. docker基本操作教程

    镜像操作 获取镜像 从Docker Hub搜索镜像: docker search ubuntu 下载镜像: docker pull ubuntu:18.04 若下载镜像速度较慢,更改镜像源: Ubun ...

  6. UiPath之Word转换为PDF

    前几天在手机上看到其他的文章,里面提到如何将Word转换为PDf,在UiPath的ManagePackage中,下载一个WordToPDF的包, 我按照上面的方法试着做了一下,但是在转换的时候很不稳定 ...

  7. NOIP模拟14-16

    最近事情有些多,先咕了! 鸽了,时间太久远了,写了话坑太大,太费时间了!

  8. 来了!GitHub for mobile 发布!iOS beta 版已来,Android 版即将发布

    北京时间 2019 年 11 月 14 日,在 GitHub Universe 2019大会上,GitHub 正式发布了 GitHub for mobile,支持 iOS 与 Android 两大移动 ...

  9. mysql忘记密码怎么办??

    1.停掉mysql 1.1单实例停止方式 [root@qiuhom ~]# /etc/init.d/mysqld stop Shutting down MySQL. [ OK ] 1.2多实例停止方式 ...

  10. 关于github 新工程上传代码 git 命令

    1.git init // 初始化git文件 2.git add . //添加上传全部文件 "."代表全部 3.git remote add origin  git····//gi ...