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的更多相关文章

  1. Ambiguous HTTP method Actions require an explicit HttpMethod binding for Swagger 2.0

    异常内容 NotSupportedException: Ambiguous HTTP method for action . Actions require an explicit HttpMetho ...

  2. netcore3.0 webapi集成Swagger 5.0,Swagger使用

    Swagger使用 1.描述 Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务. 作用: 1.接口的文档在线自动生成. 2.功能测试 本文转自 ...

  3. .Net Core Swagger:Actions require an explicit HttpMethod binding for Swagger 2.0

    添加完Swagger包引用后运行报错:Actions require an explicit HttpMethod binding for Swagger 2.0 第一时间想到了父类控制器 没有添加 ...

  4. Swagger 3.0 天天刷屏,真的香吗?

    持续原创输出,点击上方蓝字关注我 目录 前言 官方文档如何说? Spring Boot版本说明 添加依赖 springfox-boot-starter做了什么? 撸起袖子就是干? 定制一个基本的文档示 ...

  5. Swagger 2.0 集成配置

    传统的API文档编写存在以下几个痛点: 对API文档进行更新的时候,需要通知前端开发人员,导致文档更新交流不及时: API接口返回信息不明确 大公司中肯定会有专门文档服务器对接口文档进行更新. 缺乏在 ...

  6. .Netcore Swagger - 解决外部库导致的“Actions require an explicit HttpMethod binding for Swagger 2.0”

    现象: 项目中导入Ocelot后,swagger页面无法正常显示,查看异常发现 Ocelot.Raft.RaftController 中的 Action 配置不完全,swagger扫描时不能正确生成 ...

  7. spring boot:swagger3的安全配置(swagger 3.0.0 / spring security / spring boot 2.3.3)

    一,swagger有哪些环节需要注意安全? 1,生产环境中,要关闭swagger application.properties中配置: springfox.documentation.swagger- ...

  8. spring boot:swagger3文档展示分页和分栏的列表数据(swagger 3.0.0 / spring boot 2.3.3)

    一,什么情况下需要展示分页和分栏的数据的文档? 分页时,页面上展示的是同一类型的列表的数据,如图: 分栏时,每行都是一个列表,而且展示的数据类型也可能不同 这也是两种常用的数据返回形式 说明:刘宏缔的 ...

  9. spring boot:用swagger3生成接口文档,支持全局通用参数(swagger 3.0.0 / spring boot 2.3.2)

    一,什么是swagger? 1,  Swagger 是一个规范和完整的文档框架, 用于生成.描述.调用和可视化 RESTful 风格的 Web 服务文档 官方网站: https://swagger.i ...

随机推荐

  1. Unity打包安卓项目问题汇总(持续更新)

    1.V1,v2签名问题 安卓11以上--v1签名无法使用: 安卓7以下--v2无法使用: 应用宝不支持没有v1签名的包: AndroidStudio版本2020打签名包时无法勾选v1,v2选项,4.2 ...

  2. python客户端和Appium服务端联调出现的问题解决办法

    按照安装文档搭建完移动端自动化测试环境,包括:SDK.JDK.Node.js.Appium及客户端后,appium-doctor可以成功的检测到各配套版本.如下图: 可是,运行from appium ...

  3. python分支结构与循环结构

    python分支结构 一.if 单条件形式 # 年轻人的世界都不容易 age > 18 age = int(input("请输入您的年龄:")) # input()函数 模拟 ...

  4. Java中的常用类——Arrays

    数组工具类java.util.Arrays Arrays类中的方法都是static修饰的静态方法,因此可以直接使用类名.方法名来调用,而不用通过new使用对象来调用(是"不用"不是 ...

  5. 我把自己的java库发布到了maven中央仓库,从此可以像Jackson、Spring的jar一样使用它了

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  6. IPV4地址,子网掩码,子网划分

    转载自https://blog.csdn.net/qq_45108415/article/details/114179407

  7. 使用VS Code的MySQL扩展管理数据库

    我将在本文告诉你如何用VS Code的扩展程序管理MySQL数据库,包括连接到MySQL.新建数据库和表.修改字段定义.简单的查询方法以及导入导出. 在许多情况下,我们需要随时查看数据库的记录来确保程 ...

  8. es6中的导入与导出

    参考:https://www.cnblogs.com/sherrycat/p/11152994.html

  9. JavaFx 实现按钮防抖

    原文地址:JavaFx 实现按钮防抖 | Stars-One的杂货小窝 Android平台的APP,一般是有需要进行设置按钮的防抖(即在短时间内无法多次点击),我想在JavaFx项目中也是实现防抖功能 ...

  10. golang中mutex使用

    package main import ( "fmt" "sync" "time" ) /* 高并发是golang语言最大的亮点 一个线程可 ...