swagger接口文档使用

swagger接口文档

    • 一,swagger简介
      • 前后端分离
      • swagger 诞生
    • 二,springboot集成swagger
      • 依赖
      • 编写helloworld接口
      • 配置swagger ==> config 配置类
      • 测试运行
    • 三,配置swagger
      • swagger 配置扫描接口
      • 如何做到只在生产环境中启动swagger?
      • 配置api文档的分组
    • 四,swagger注解用于对实体类,接口的描述
      • pojo
      • controller
    • 五,你以为的是swagger只能提供接口描述信息?呵~
      • swagger能测试呀
    • 六,最后

一,swagger简介

前后端分离

vue + springboot 一套解决方案

后端时代:前端只用管理静态页面;html===>后端。模板引擎 jsp ejb 后端是主力

前后端分离时代:

  • 后端:后端控制层,服务层,数据访问层 【后端团队】
  • 前端:前端控制层(双向绑定),视图层【前端团队】
    • 伪造后端数据,json。已经存在,无需后端,全段工程依旧能够跑起来。
  • 前后端如何交互?===> API json
  • 前后端相对独立,松耦合
  • 前后端可用部署在不同的服务器上

前后端分离产生,一个问题

  • 前后端集成联调,前端和后端人员无法做到,”即时协商,尽早解决“,最终导致问题集中爆发

解决方法:

  • 指定schema 【计划的提纲】。实时更新最新api,降低集成的风险;

  • 早些年:制定word计划文档;

  • 前后端分离:

    • 前端测试后端接口:postman
    • 后端提供接口,需要实时更新最新的消息及改动!

swagger 诞生

  • 号称世界上最流行的api框架
  • restful api文档在线自动生成工具=> api 文档与api定义同步更更新
  • 直接运行,在线测试api接口
  • 支持多种语言 java,php,py…

官网:https://swagger.io

在项目中使用swagger springbox

  • swagger2
  • ui

二,springboot集成swagger

依赖

  • 导入相关依赖

    • springfox-swagger2
    • springfox-swagger-ui
  • pom

    <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
    <dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.9.2</version>
    </dependency><!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
    <dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.9.2</version>
    </dependency>
    

编写helloworld接口

package cn.bitqian.swagger.controller;import org.springframework.web.bind.annotation.*;/*** hello world* @author echo lovely* @date 2020/10/28 19:23*/
@RestController
public class HelloController {@RequestMapping(value = "/hello")public String hello() {return "hello";}}

配置swagger ==> config 配置类

开启swagger,Enablexxx

@Configuration
@EnableSwagger2 // 开启swagger2
public class SwaggerConfig {}

测试运行

http://localhost:8080/swagger-ui.html

三,配置swagger

来告诉交接人员,接口是干嘛的,对controller的接口进行描述。也可以设置接口访问权限。

package cn.bitqian.swagger.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;import java.util.ArrayList;/*** swagger配置类* @author echo lovely* @date 2020/10/28 19:35*/
@Configuration
@EnableSwagger2 // 开启swagger2
public class SwaggerConfig {// 文档的标题,和描述!作者的信息deng..@Beanpublic Docket docket() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(appInfo());}// api 信息 接口文档的头部信息描述public ApiInfo appInfo() {Contact contact = new Contact("bitqian", "http://example.com", "admin-@www.example.cn");return new ApiInfo("bitqian的swagger文档","用来描述此接口的功能,内容。","v1.0","http://example.cn", contact,"Apache 2.0","http://www.apache.org/licenses/LICENSE-2.0",new ArrayList());}}

swagger 配置扫描接口

@Beanpublic Docket docket() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(appInfo()).select().// RequestHandlerSelectors 配置要扫描的接口的方式// basePackage 指定要扫描的包// any() 扫描全部// none() 不扫描// withClassAnnotation(Controller.class)  扫描类上的注解, 生效// withMethodAnnotation(RequestMapping.class)  扫描方法上的注解, 生效apis(RequestHandlerSelectors.basePackage("cn.bitqian.swagger.controller")).// paths(PathSelectors.ant("/bitqian/**"))  扫描指定的接口// PathSelectors.regex("")paths(PathSelectors.ant("/hello/**")).build();}

如何做到只在生产环境中启动swagger?

  • 判断是否是生产环境
  • 注入enable
    @Beanpublic Docket docket(Environment environment) {// 获取当前环境 是生产环境 启动swaggerboolean isProFlag = environment.acceptsProfiles(Profiles.of("pro"));System.out.println("is dev environment....===========>" + isProFlag);return new Docket(DocumentationType.SWAGGER_2).apiInfo(appInfo()).groupName("bitqian").enable(isProFlag). // 是否启动swagger 如果为false,则不能在浏览器中使用swaggerselect().apis(RequestHandlerSelectors.basePackage("cn.bitqian.swagger.controller")).// paths(PathSelectors.ant("/hello/**")).build();}

配置api文档的分组

  • 整出多个docket,多个人写不同的接口
@Bean
public Docket docket1() {return new Docket(DocumentationType.SWAGGER_2).groupName("bitqian666 group1");
} @Bean
public Docket docket12() {return new Docket(DocumentationType.SWAGGER_2).groupName("bitqian666 group2");
}

四,swagger注解用于对实体类,接口的描述

pojo

package cn.bitqian.swagger.entity;import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;/*** 给实体类生成文档* @author echo lovely* @date 2020/10/29 21:09*/
@ApiModel("user实体类")
public class User {@ApiModelProperty("用户名")public String username;@ApiModelProperty("密码")public String password;public User() {}public User(String username, String password) {this.username = username;this.password = password;}
}

controller

package cn.bitqian.swagger.controller;import cn.bitqian.swagger.entity.User;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;/*** hello world* @author echo lovely* @date 2020/10/28 19:23*/
@RestController
public class HelloController {@ApiOperation("get/post都可的hello接口")@RequestMapping(value = "/hello")public String hello() {return "hello";}@ApiOperation("get的hello接口, 返回一个空 user")@GetMapping(value = "/hello1")public User hello1() {return new User();}@ApiOperation("hello1 接口post way~")@PostMapping(value = "/hello1")public User hello1(@ApiParam("传递用户") User user) {return user;}@ApiOperation("登录接口 post way~")@PostMapping(value = "/login")public String login(@ApiParam("登录用户名") @RequestParam("username") String username,@ApiParam("登录密码")  @RequestParam("password") String password) {return "ok" + "{" + username + ", " + password + "}";}}

五,你以为的是swagger只能提供接口描述信息?呵~

swagger能测试呀

try it out


六,最后

  1. 我们可用通过swagger给一些比较难理解的属性或者接口,增加注释信息
  2. 接口文档实时更新
  3. 可以在线测试

swagger是一个优秀的工具,几乎所有大公司都有使用它,更符合迭代开发的需求。

【注意点】 在正式发布的时候,关闭swagger!!!处于安全考虑。而且节省运行的内存。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/420553.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

异步任务,邮箱任务,定时任务

task~异步任务邮箱任务定时任务源码下载异步任务 开启多线程&#xff0c;我飞了。 package cn.bitqian.service;import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service;/*** 异步任务 目的 多线程&#xff0c;交给spring托…

iframe在ipad safari的显示

今 天要在web中嵌套一个网址或本地HTML&#xff0c;用到了iframe&#xff0c;在电脑上设置scrolling‘auto’&#xff0c;宽度高度&#xff0c;会有滚动条出现。而在 ipad上会全部显示整个网页的宽度高度。scrolling属性无效。原来在html5中的iframe已经只有剩下src的属性。 但…

IOS 开发中 Whose view is not in the window hierarchy 错误的解决办法

在 IOS 开发当中经常碰到 whose view is not in the window hierarchy 的错误&#xff0c;该错误简单的说&#xff0c;是由于 "ViewController" 还没有被加载&#xff0c;就调用该 ViewController 或者 ViewController 内的方法时&#xff0c;就会报这个错误。在不同…

maven传递依赖

目录1. 依赖传递2. 什么是依赖冲突3. 怎么解决4. 项目聚合maven是一个项目管理的工具&#xff0c;从项目的构建到项目开发&#xff0c;再到项目的测试&#xff0c;项目上线&#xff0c;都可一键管理。1. 那么&#xff0c;还有maven是如何管理项目中所用到的jar版本冲突&#xf…

使用apache FileUtils下载文件

目录工具代码实现测试工具 <dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.5</version></dependency>或者 https://mvnrepository.com/artifact/commons-io/commons-io/2.7 然后放…

二分图多重匹配

在二分图的最大匹配中&#xff0c;每个点&#xff08;不管是X集合还是Y集合&#xff09;最多只能与一条匹配边相关联&#xff0c; 然而&#xff0c;经常有这种问题&#xff0c;即二分图的一个点可以和多条匹配边相关联&#xff0c;但有上限&#xff0c;即cap[i]表示点i最多能和…

springmvc,spring,hibernate5.0整合

目录1. pom依赖2. web.xml3. spring核心配置文件3.1 jdbc配置信息3.2 sping 配置文件4. 实体映射5. 项目结构5.1 curd5.2 页面6. 测试1. spring版本 5.1.5 RELEASE 2. hibernate版本 5.3.9.Final 3. 数据源使用c3p0项目使用eclipse2017 maven构建, 完成学生的新增&#xff0c;…

MYSQL 查看表上索引的 1 方法

前期准备&#xff1a; create table T9(A int ,B text,C text,fulltext index fix_test_for_T8_B(B));#在定义表的时候加索引 create unique index ix_test_for_T8_A on T9(A);#加朴素索引 create fulltext index fix_test_for_T8_C on T9(C);#加全文索引 --------------------…

springmvc 结合ajax批量新增

目录1. 需要注意的问题2. 页面代码3. controller定义参数接收1. 需要注意的问题 mvc框架的处理日期问题ResponseBody响应对象是自定义对象&#xff0c;响应不是jsonResopnseBody响应自定义对象时&#xff0c;日期为是long类型的数结束数据方法的参数&#xff0c;该如何定义&am…

吐槽一下Activiti的用户手册和一本书

业余没事的时候&#xff0c;看了点Java的资料&#xff0c;无意之中发现了Activiti&#xff0c;就打算自己跑几个例子看看到底是怎么回事。一直搞底层&#xff0c;也得偶尔关心下上层到底发展到什么程度了不是。悲惨的过程就是这么开始的&#xff0c;首先是Activiti的用户手册&a…

手写简单的启动器

starter1. target2. 手写启动器~2.1 自动装配&#xff0c;自定义属性2.2 启动器&#xff0c;引用自动装配模块3. 在自己的项目引用上面的starter1. target 1. 启动器只用来做依赖导入(导入配置模块)2. 专门来写一个自动配置模块3. 启动器依赖自动配置&#xff1b;别人只需要引入…

Android 颜色渲染(九) PorterDuff及Xfermode详解

Android 颜色渲染(九) PorterDuff及Xfermode详解之前已经讲过了除ComposeShader之外Shader的全部子类, 在讲ComposeShader(组合渲染)之前, 由于构造ComposeShader需要 PorterDuffXfermode或者PorterDuff.Mode作为参数,所以在此先详细地了解下这两个类的作用,这对之后的绘图会…

拦截器(二)

只拦截controller的请求, 基于aop&#xff0c;横切。 Spring MVC的拦截器类似于Servlet开发中的过滤器Filter&#xff0c; 用于对处理器进行预处理和后处理。 将拦截器按一定的顺序联结成一条链&#xff0c; 这条链称为拦截器链&#xff08;InterceptorChain&#xff09;。 在访…

nodejsmongoangularjs

http://www.ibm.com/developerworks/cn/web/wa-nodejs-polling-app/转载于:https://www.cnblogs.com/xixifusigao/p/4037928.html

每次新建Android项目都报样式找不到的错误?

问题描述如图再网上找了下说改为<style name"AppBaseTheme" parent"android:Theme.Light">这样就行了的确改为这样就ok了但是如果每次都要这么改&#xff0c;不是很烦&#xff1f;有没有彻底解决这个问题的方法&#xff1f;谢谢 解决方案1新建的时候…

spring mvc全局异常处理,注解实现

ssm框架中的异常处理&#xff0c;可以是dao, service, controller 一直抛出异常&#xff0c;抛出就完事了。最终由全局异常类捕获&#xff0c;进行日志记录&#xff0c;页面跳转。… 核心注解 // 方法级别 ExceptionHandler // 全局异常类上 ControllerAdvice // ControllerA…

Qt多线程学习:创建多线程

【为什么要用多线程&#xff1f;】 传统的图形用户界面应用程序都仅仅有一个运行线程&#xff0c;而且一次仅仅运行一个操作。假设用户从用户界面中调用一个比較耗时的操作&#xff0c;当该操作正在运行时&#xff0c;用户界面一般会冻结而不再响应。这个问题能够用事件处理和多…

sql server使用杂记

SqlServer导出数据库 navcat for sql server中打开连接&#xff0c;打开数据库&#xff0c;右键--数据传输&#xff0c;常规选项卡--模式选择dbo&#xff0c;目标选择连接&#xff08;选择你新建的库&#xff09;或者文件&#xff08;导出你要的sql文件位置&#xff09;&#x…