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. 【解决了一个小问题】go.mod文件中引用另一个库,总会自动拉取新版本

    我的项目依赖某个旧的公共库: require ( git.xxx.com/myprj/mylib v0.0.43 ) 可以编译的时候,系统总会自动加上这样的路径: require ( git.xxx. ...

  2. 【get√】golang新手理解了一点点gin框架的中间件

    1.通过use()来使用多个中间件 router := gin.New() router.Use( middleware1, middleware2) 2.中间件的典型实现 func Logger() ...

  3. gin框架中项目的初始化

    核心知识点 json配置文件解析成结构体 将路由对应的接口抽离到单独的文件中,main函数中直接注册路由即可 项目目录图 项目代码 app.json代码 { "app_name": ...

  4. 不难懂-----redux

    一.flux的缺陷 因为dispatcher和Store可以有多个互相管理起来特别麻烦 二.什么是redux 其实redux就是Flux的一种进阶实现.它是一个应用数据流框架,主要作用应用状态的管理 ...

  5. Windows mysql免安装版配置。(版本号-5.6.45);

    Windows mysql免安装版配置.(版本号-5.6.45); 来自对 https://blog.csdn.net/weixin_42831477/article/details/81325691 ...

  6. Java枚举类在生产环境中的使用方式

    前言   Java枚举在项目中使用非常普遍,许多人在做项目时,一定会遇到要维护某些业务场景状态的时候,往往会定义一个常量类,然后添加业务场景相关的状态常量.但实际上,生产环境的项目中业务状态的定义大部 ...

  7. 如何美化 Matplotlib 3D坐标系

    前言 ~mpl_toolkits.mplot3d 生成的3D坐标系背景色是灰色的,刻度线也向内延伸了,如果搭配上其他白色背景的 2D 图,看起来很奇怪,比如下面这张图: 网上有一些办法可以将3D坐标区 ...

  8. vue组件中使用watch响应数据

    在vue中,使用watch来响应数据的变化.watch的用法大致有三种.下面代码是watch的一种简单的用法: 普通用法: <template> //视图 <input v-mode ...

  9. js 每日一更(数组转换成前端更容易解析的树状结构)

    <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content ...

  10. ABC209 E Shiritori

    考虑对这个问题进行转化: 显然我们只关注每个串前三个棋子和后三个棋子,并且根据题目的特性,我们可以将任意的三个字符看作点,将一个字符串看作连接两个点的边,这样我们得到了一张点数为 \(52 ^ 3\) ...