swagger 2.0
1、引入jar包
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
2、配置swagger配置文件
import io.swagger.annotations.Api;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2; /**
* @author xiufengd
* @date 2021/8/10 10:59
* @description 用于生成API接口文档的配置
* @note 未来可期
*/
@Configuration
@EnableSwagger2
//@EnableWebMvc 注掉默认的,改成继承WebMvcConfigurationSupport,重写addResourceHandlers,将swagger-ui.html映射出来
public class SwaggerConfig extends WebMvcConfigurationSupport { @Bean
public Docket petApi() { ParameterBuilder ticketPar = new ParameterBuilder(); return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
.apis(RequestHandlerSelectors.basePackage("com.dxf.controller")) //指定提供接口所在的基包
.build();
} /**
* 该套 API 说明,包含作者、简介、版本、host、服务URL
* @return
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("接口文档")
.version("0.1")
.description("该接口文档暂时只用于前后端调试对接使用,切勿对外暴露!")
.build();
}
//将swagger-ui.html 映射到项目中可以直接访问
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations(
"classpath:/static/");
registry.addResourceHandler("swagger-ui.html").addResourceLocations(
"classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations(
"classpath:/META-INF/resources/webjars/");
super.addResourceHandlers(registry);
} }
3、定义返回类型
package com.xiufengd.utils; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; /**
* @author xiufengd
* @date 2021/8/13 9:32
* @description
* @note 未来可期
*/
@ApiModel(value="Result返回对象",description="Result返回对象")
public class Result {
// 响应业务状态
@ApiModelProperty(value="状态",name="status",example="200")
private Integer status; // 响应消息
@ApiModelProperty(value="响应消息",name="msg",example="ok")
private String msg; // 响应中的数据
@ApiModelProperty(value="响应数据",name="data",example="[{\"floor\":1},{\"floor\":2}]")
private Object data; public Integer getStatus() {
return status;
} public void setStatus(Integer status) {
this.status = status;
} public String getMsg() {
return msg;
} public void setMsg(String msg) {
this.msg = msg;
} public Object getData() {
return data;
} public void setData(Object data) {
this.data = data;
}
}
4、在类上注解写到死import io.swagger.annotations.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @author xiufengd
* @date 2021/8/6 17:59
* @description
* @note 未来可期
*/
@Api(value = "测试接口类",tags ="测试接口类")
@Controller("test")
@RequestMapping("test")
public class test {
@ApiOperation(value = "定义测试接口方法")
@GetMapping("test")
//定义返回
@ApiResponses({
@ApiResponse(code = 200, message = "{\"status\":200,\"msg\":\"ok\",\"data\":\"success data\"}"),
@ApiResponse(code = 201, message = "{\"status\":201,\"msg\":\"ok\",\"data\":\"error data\"}")
})
@ResponseBody
public Result test(@RequestParam @ApiParam("参数名") String param) throws Exception {
Result result = new Result();
result.setStatus(200);
result.setData("success data");
result.setMsg("ok");
return result;
}
}
5、shiro放开权限
<!-- swagger配置 -->
/swagger-ui.html=anon
/swagger-resources=anon
/v2/api-docs=anon
/webjars/springfox-swagger-ui/**=anon
6、页面访问
http://127.0.0.1:8080/swagger-ui.html
swagger 2.0的更多相关文章
- Ambiguous HTTP method Actions require an explicit HttpMethod binding for Swagger 2.0
异常内容 NotSupportedException: Ambiguous HTTP method for action . Actions require an explicit HttpMetho ...
- netcore3.0 webapi集成Swagger 5.0,Swagger使用
Swagger使用 1.描述 Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务. 作用: 1.接口的文档在线自动生成. 2.功能测试 本文转自 ...
- .Net Core Swagger:Actions require an explicit HttpMethod binding for Swagger 2.0
添加完Swagger包引用后运行报错:Actions require an explicit HttpMethod binding for Swagger 2.0 第一时间想到了父类控制器 没有添加 ...
- Swagger 3.0 天天刷屏,真的香吗?
持续原创输出,点击上方蓝字关注我 目录 前言 官方文档如何说? Spring Boot版本说明 添加依赖 springfox-boot-starter做了什么? 撸起袖子就是干? 定制一个基本的文档示 ...
- Swagger 2.0 集成配置
传统的API文档编写存在以下几个痛点: 对API文档进行更新的时候,需要通知前端开发人员,导致文档更新交流不及时: API接口返回信息不明确 大公司中肯定会有专门文档服务器对接口文档进行更新. 缺乏在 ...
- .Netcore Swagger - 解决外部库导致的“Actions require an explicit HttpMethod binding for Swagger 2.0”
现象: 项目中导入Ocelot后,swagger页面无法正常显示,查看异常发现 Ocelot.Raft.RaftController 中的 Action 配置不完全,swagger扫描时不能正确生成 ...
- spring boot:swagger3的安全配置(swagger 3.0.0 / spring security / spring boot 2.3.3)
一,swagger有哪些环节需要注意安全? 1,生产环境中,要关闭swagger application.properties中配置: springfox.documentation.swagger- ...
- spring boot:swagger3文档展示分页和分栏的列表数据(swagger 3.0.0 / spring boot 2.3.3)
一,什么情况下需要展示分页和分栏的数据的文档? 分页时,页面上展示的是同一类型的列表的数据,如图: 分栏时,每行都是一个列表,而且展示的数据类型也可能不同 这也是两种常用的数据返回形式 说明:刘宏缔的 ...
- spring boot:用swagger3生成接口文档,支持全局通用参数(swagger 3.0.0 / spring boot 2.3.2)
一,什么是swagger? 1, Swagger 是一个规范和完整的文档框架, 用于生成.描述.调用和可视化 RESTful 风格的 Web 服务文档 官方网站: https://swagger.i ...
随机推荐
- vmware快速扩容虚拟磁盘
在使用vmware进行虚拟化的时候,会遇到虚拟磁盘不够用的情况,以前的办法都是使用lvm进行管理扩容,目前在linux上可以实现快速扩容了,具体方法如下: 该方法参考阿里云在线扩容文档:文档地址 其中 ...
- Rust 实现一个简单的区块链
一.背景 近期用 Rust 实现了 Jeiwan/blockchain_go,与原项目相比没有加入新的功能,只是换了一个编程语言实现了一遍,源码放在 Github 上. 开发这个项目,花费了好几个周末 ...
- 【解决了一个小问题】alert manager中的cluster.advertise-address参数是什么意思?
如果在启动 alert manager的时候,不填写参数: /usr/bin/alertmanager --config.file=/etc/alert_manager/alertmanager.ya ...
- 【记录一个问题】铁威马NAS存储中的人人影视APP,其WEB服务占满一个CPU核
终端登录后,top命令发现rrshareweb这个进程把单个CPU核占满了. 发现其实是人人影视的web服务,而这个服务里面我还根本未使用. 卸载这个app后正常.
- Go语言:包管理基础知识
起因是,遇到一个问题: 经查阅资料,很可能跟包管理有关,之前有了解过忘了就再学一遍顺便解决问题. 学习资料: GO111MODULE 是个啥? - 知乎 (zhihu.com) go mod使用 - ...
- MySQL环境变量配置方法
MySQL配置方法 下载免安装版本的MySQL数据库,大家根据自己的开发环境下载对应版本的数据库,我在此举例的是Windows系统下的配置方法,下载地址如下: https://dev.mysql.co ...
- epoll(1)
#include<stdio.h> #include<fcntl.h> #include<unistd.h> #include<sys/epoll.h> ...
- gin中在中间件或handler中使用goroutine
package main import ( "fmt" "github.com/gin-gonic/gin" "log" "tim ...
- linux中uniq命令全面解析
目录 一 :uniq作用 二:uniq格式 1.参数 2.参数案例解析: linuxuniq简介 Linux uniq 命令用于检查及删除文本文件中重复出现的行列,一般与 sort 命令结合使用. u ...
- 强化学习实战 | 自定义Gym环境之扫雷
开始之前 先考虑几个问题: Q1:如何展开无雷区? Q2:如何计算格子的提示数? Q3:如何表示扫雷游戏的状态? A1:可以使用递归函数,或是堆栈. A2:一般的做法是,需要打开某格子时,再去统计周围 ...