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. 学习笔记_58 python语法基础

    1.python是解析型语言. 有点像javaScript在html运行一样,不需要mian函数入口,随时随地定义函数,执行函数, 执行语句,定义类型 2.python能面向对象 3.python使用 ...

  2. 《吊打面试官》系列-Redis终章_凛冬将至、FPX_新王登基

    你知道的越多,你不知道的越多 点赞再看,养成习惯 前言 Redis在互联网技术存储方面使用如此广泛,几乎所有的后端技术面试官都要在Redis的使用和原理方面对小伙伴们进行360°的刁难.作为一个在互联 ...

  3. 爬虫学习--Urllib库基本使用 Day1

    一.Urllib库详解 1.什么是Urllib Python内置的HTTP请求库 urllib.request    请求模块(模拟实现传入网址访问) urllib.error             ...

  4. Project Euler 55: Lychrel numbers

    五十五.吕克雷尔数(Lychrel numbers) 如果我们把\(47\)翻转过来并和其自身相加,结果是\(47+74=121\)是一个回文数.并不是所有的数都可以这么快的变成回文数,比如说: \[ ...

  5. JVM初体验

    一.设计堆内存溢出异常:OutOfMemoryError: public class Main { public static void main(String[] args) { List<D ...

  6. L1与L2正则化的对比及多角度阐述为什么正则化可以解决过拟合问题

    正则化是一种回归的形式,它将系数估计(coefficient estimate)朝零的方向进行约束.调整或缩小.也就是说,正则化可以在学习过程中降低模型复杂度和不稳定程度,从而避免过拟合的危险. 一. ...

  7. python入门斐波那契数列之迭代,递归

    迭代 def fab(n): a1=1 a2=1 a3=1 if n < 1 : print("输入有误!") return -1 while n-2 > 0 : a3 ...

  8. avtivmq(订阅写法)

    发布-订阅消息模式与点对点模式类似,只不过在session创建消息队列时,由session.createQuene()变为session.createTopic(). 消息发布者代码: 消息订阅者代码 ...

  9. nyoj 35-表达式求值(stack, 栈的应用)

    35-表达式求值 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:37 submit:53 题目描述: ACM队的mdd想做一个计算器,但是,他要做的 ...

  10. 使用不同的C++支持库的模块混合开发时,引发异常展开不正常,抛异常竟引出一个SIGSEGV

    如果你使用gcc对一部分模块进行了GNUMake的编译,这些编译出动态库使用在Gradle编译框架下的项目.那么就有可能出现题目中的情况,使用不同的C++支持库的模块混合开发时,引发异常展开不正常. ...