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. echart实现实时疫情图

    直接上代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> </h ...

  2. vue.config.js报错cannot set property "preserveWhitespace" of undefined

    vue.config.js报错cannot set property "preserveWhitespace" of undefined 最近在项目中配置webpack,由于vue ...

  3. rocketmq之延迟队列(按照18个等级来发送)

    1 启动消费者等待传入的订阅消息 import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer; import org.apache ...

  4. k8s中kubeconfig的配置及使用

    1.概述 kubeconfig文件保存了k8s集群的集群.用户.命名空间.认证的信息.kubectl命令使用kubeconfig文件来获取集群的信息,然后和API server进行通讯. 注意:用于配 ...

  5. Git命令中波浪号~与脱字符^的区别

    0.前言 波浪号~,英文名叫 tilde.脱字符^,英文名叫caret. 这两种符号常见于git reset的情景,简单的项目结构和操作一般不会涉及到两者之间的区别,似乎用哪个都可以.如果遇到比较繁杂 ...

  6. 【笔记】macos上部署thanos_receiver + thanos_query

    为了方便起见,在mac笔记本上进行了测试 1.写一个发送数据的客户端 package main import ( "fmt" "io/ioutil" " ...

  7. JUC之文章整理以及汇总

    JUC文章汇总 JUC部分将学习<JUC并发编程的艺术>和<尚硅谷-大厂必备技术之JUC并发编程>进行博客的整理,各文章中也会不断的完善和丰富. JUC概述 JUC的视频学习和 ...

  8. C++多线程之互斥锁和超时锁

    #include<iostream> #include<thread> #include<mutex> using namespace std; mutex mu; ...

  9. gin框架使用Air实时加载

    Air实时加载 本章我们要介绍一个神器--Air能够实时监听项目的代码文件,在代码发生变更之后自动重新编译并执行,大大提高gin框架项目的开发效率. 1.1.1. 为什么需要实时加载? 之前使用Pyt ...

  10. JavaScript实现禁止打开控制台

    通过 JavaScript 实现禁止打开控制台(期中包括:右键审查元素.工具栏.F12.Shift+Ctrl+I) <!DOCTYPE html> <html lang=" ...