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. 从AAB文件生成APK文件

    开头先说方法 1.下载 bundletool 2.开始生成 java -jar bundletool.jar build-apks --bundle=[aab文件路径(例如:C:\Users\Admi ...

  2. Scratch 3 矢量编辑器——“临摹”一只哆啦A梦

    利用Scratch来制作一些小作品,常常需要到网上去找图片,而网上下载的图片一般都是位图,往往存在两个问题: 图片不够清晰,当图片放大后会出现"马赛克"现象: 图片中存在不必要的背 ...

  3. go语言 strconv.ParseInt 的实现分析

    字符串与数值之间进行转换是一个高频操作,在go语言中,SDK提供 strconv.ParseInt 将字符串转换为数值,strconv.FormatInt 可以将数值转换为字符串. 1.首先看下 st ...

  4. 2月1日 体温APP开发记录

    1.阅读构建之法 现代软件工程(第三版) 2.观看Android开发视频教程最新版 Android Studio开发

  5. 用Win +R运行快速启动各种程序

    许多人认为Windows的Win+R运行就是摆设,除了开cmd和shutdown外毫无用处.其实Win+R是可以用于各种快捷启动的. Win+R可以视作执行一条cmd命令,要用他运行程序,理论上必须输 ...

  6. Web开发底层是Servlet

    SpringMVC:是基于spring的一个框架,实际上就是spring的一个模块,专门是做web开发. 可以理解成servlet是一个升级 web开发底层是servlet,框架是在servlet基础 ...

  7. 集合框架-TreeSet-Comparator比较器练习(字符串长度排序)

    1 package cn.itcast.p5.treeset.test; 2 3 import java.util.Iterator; 4 import java.util.TreeSet; 5 6 ...

  8. linux 创建用户 用户组,sudo,禁止root远程ssh登录

    创建用户  useradd hanli 为新用户设置密码(在root下可以为普通用户重置密码)  passwd hanli 创建用户组  groupadd  op 将用户添加到用户组  usermod ...

  9. 初步学习TypeScript

    使用Visual Studio Code搭建TypeScript开发环境 1.TypeScript是干什么的 ? TypeScript是由微软Anders Hejlsberg(安德斯·海尔斯伯格,也是 ...

  10. python19day

    内容回顾 软件开发规范 预计 递归函数1天 re模块2天 logging模块+包的导入+带参数的装饰器1-2天 面向对象6天 网络编程4天 并发编程5-6天 数据库5天 今日内容 模块和实际工作之间的 ...