引言

最近在后台开发的时候,使用swagger2进行前后台接口文档的声明。由此遇见的一些问题,写下来给自己复习。

参考:

https://blog.csdn.net/xupeng874395012/article/details/68946676

正文

在进行整合swagger2的时候,首先引入swagger2的jar,由于我使用的是springboot,所以以springboot为例。

<!--springboot 集成 swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.5.0</version>
</dependency>

引入swagger2的jar包之后,我们需要配置一个swagger2的配置类,来声明一些swagger2的配置信息


这样的话,swagger2就已经配置完毕了。接下来你只需要在你的接口上配置你想要显示的信息即可。

@Configuration  //表示是配置类,要被加载
@EnableSwagger2 //swagger的配置
public class Swagger2 { @Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) //添加ApiOperiation注解的被扫描
.paths(PathSelectors.any())
.build();
} private ApiInfo apiInfo(){
Contact contact = new Contact("xx", "www.baidu.com", "xxx@126.com");
return new ApiInfoBuilder()
.title("cc").contact(contact).description("接口文档").license("Apache License Version 2.0")
.version("v1.0").build();
}
}

接口上信息的配置如下:

@RestController
@Api(value = "user", tags = "用户模块")
@RequestMapping("user")
public class UserController extends BaseController { @Resource
private UserService userService; @ApiOperation(value = "用户添加")
@ApiImplicitParams({
@ApiImplicitParam(name = "user",value = "用户" ,required = true,dataType = "String", paramType = "body")
})
@RequestMapping(value = "addUser",method = RequestMethod.POST)
public ResultBean addUser(User user){
return resultBean;
}
}

关于其中@Api和@ApiOperation等的详细解释如下:

作用范围 API 使用位置
对象属性 @ApiModelProperty 用于出入参数对象的字段上
协议集描述 @Api 用于Controller类上
协议描述 @ApiOperation 用在Controller的方法上
Response集 @ApiResponses 用在controller的方法上
Response @ApiResponse 用在 @ApiResponses里边
非对象参数集 @ApiImplicitParams 用在controller的方法上
非对象参数描述 @ApiImplicitParam 用在@ApiImplicitParams的方法里边
描述返回对象的意义 @ApiModel 用在返回对象类上

关于参数的详细解释

属性 取值 作用
paramType   查询参数类型
  path 以地址的形式提交数据
  query 直接跟参数完成自动映射赋值
  body 以流的形式提交 仅支持POST
  header 参数在request headers 里边提交
  form 以form表单的形式提交 仅支持POST
dataType   参数的数据类型 只作为标志说明,并没有实际验证
  Long  
  String  
name   接收参数名(必须与方法中参数名一致)
value   接收参数的意义描述(描述信息)
required   参数是否必填
  true 必填
  false 非必填
defaultValue   默认值

关于swagger文档的使用方法的更多相关文章

  1. Swagger文档转Word 文档

    GitHub 地址:https://github.com/JMCuixy/SwaggerToWord/tree/developer 原创作品,转载请注明出处:http://www.cnblogs.co ...

  2. 利用typescript生成Swagger文档

    项目地址:https://github.com/wz2cool/swagger-ts-doc demo代码地址:https://github.com/wz2cool/swagger-ts-doc-de ...

  3. 使用 Swagger 文档化和定义 RESTful API

    大部分 Web 应用程序都支持 RESTful API,但不同于 SOAP API——REST API 依赖于 HTTP 方法,缺少与 Web 服务描述语言(Web Services Descript ...

  4. Swagger文档转Word

    Swagger文档转Word 文档   GitHub 地址:https://github.com/JMCuixy/SwaggerToWord/tree/developer 原创作品,转载请注明出处:h ...

  5. Spring Boot:整合Swagger文档

    综合概述 spring-boot作为当前最为流行的Java web开发脚手架,越来越多的开发者选择用其来构建企业级的RESTFul API接口.这些接口不但会服务于传统的web端(b/s),也会服务于 ...

  6. API接口文档中将Swagger文档转Word 文档

    一般的开发工作,尤其是API接口的开发工作,首先要有开发文档,接口说明文档 ok,后来开发完毕了 和页面联调,或者是和第三方联调的时候, 这个时候,SA systeam admin 就会开始直接让开发 ...

  7. Core + Vue 后台管理基础框架8——Swagger文档

    1.前言 作为前后端分离的项目,或者说但凡涉及到对外服务的后端,一个自描述,跟代码实时同步的文档是极其重要的.说到这儿,想起了几年前在XX速运,每天写完代码,还要给APP团队更新文档的惨痛经历.给人家 ...

  8. SpringBoot系列:六、集成Swagger文档

    本篇开始介绍Api文档Swagger的集成 一.引入maven依赖 <dependency> <groupId>io.springfox</groupId> < ...

  9. 如何Spring Cloud Zuul作为网关的分布式系统中整合Swagger文档在同一个页面上

    本文不涉及技术,只是单纯的一个小技巧. 阅读本文前,你需要对spring-cloud-zuul.spring-cloud-eureka.以及swagger的配置和使用有所了解. 如果你的系统也是用zu ...

随机推荐

  1. codeforces242E XOR on Segment

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  2. ubuntu下python安装pandas和numpy等依赖库版本不兼容的问题RuntimeWarning: numpy.dtype size changed

    习惯了linux下用pip install numpy及pip install pandas命令了.折腾了好久了. 上来先在python3中pip3 install numpy装了numpy,然后再p ...

  3. 在xampp集成环境下使用 php 连接oracle

    今天搞了大半天,终于成功了. 1. 首先需要让xampp支持oracle,直接按这个网页上说的做就行.http://nimal.info/blog/2009/activate-oracle-on-xa ...

  4. PWA web应用模型

    2018年的第一篇博客,最近都去挤图书馆了,希望新年新气象... 简介 PWA 是一门Google推出的web前端新技术,全称是Progressive Web App,是Google在2015年提出, ...

  5. vue结合element-ui 的select 全选问题

    下拉列表多选 问题 通过操作 所有来进行全选 全不选问题 element-ui 中 select 记录下自己最近使用element-ui 中的 select多选问题 在element中默认是指单纯多选 ...

  6. tcping的安装和使用

    1.LINUX安装方法: 编译安装下载地址: http://linuxco.de/tcping/tcping.html tar zxvf tcping-1.3.5.tar.gz cd tcping-1 ...

  7. C++编译错误:multiple types in one declaration

    这是在使用QT的时候看到的.这种情况往往是结构体或者是class最后少加了一个分好,加上即可,这个bug找了我好久,mark一下.

  8. 【Seajs源码分析】3. 工具方法2

    util-request.js 动态加载模块 /** * util-request.js - The utilities for requesting script and style files * ...

  9. html hidefocus="true"

    最近学到html,看到别人写的代码带hidefocus="true",查了一下是使超链接不显示周围的虚线. hideFocus即隐藏聚焦,具有使对象聚焦失效的功能,其功能相当于: ...

  10. vue.js 源代码学习笔记 ----- 工具方法 perf

    import { inBrowser } from './env' export let mark export let measure if (process.env.NODE_ENV !== 'p ...