springfox Swagger 2

Springfox Swagger2从入门到精通 Swagger 是一种用于设计、构建、文档化和使用 RESTful API 的工具。Springfox 是 Swagger 在 Spring 应用中的集成库,提供了自动生成 API 文档的功能。在本文中,我们将探讨如何使用 Springfox Swagger2 在 Spring Boot 项目中生成、配置和使用 API 文档。 阅读详情
<!-- 构建Restful API -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.6.0</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.6.0</version>
</dependency>

2.Swagger配置文件

package com.custom.web.swagger;

import com.google.common.base.Predicate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;

import static com.google.common.base.Predicates.or;
import static com.google.common.collect.Lists.newArrayList;
import static springfox.documentation.builders.PathSelectors.regex;

@Configuration    // 配置注解,自动在本类上下文加载一些环境变量信息
@EnableSwagger2   // 使swagger2生效
@EnableWebMvc
@ComponentScan(basePackages = {"com.custom.web"})  //需要扫描的包路径
public class SwaggerConfig extends WebMvcConfigurationSupport {

    @Bean
    public Docket swaggerSpringMvcPlugin() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("business-api")
                .select()   // 选择那些路径和api会生成document
.apis(RequestHandlerSelectors.basePackage("com.custom.web"))
                .paths(paths())
                //.apis(RequestHandlerSelectors.any())  // 对所有api进行监控
                //.paths(PathSelectors.any())   // 对所有路径进行监控
.build()
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts());
    }

    private Predicate<String> paths() {
        return or(regex("/person.*"));
    }

    private List<ApiKey> securitySchemes() {
        return newArrayList(
                new ApiKey("clientId", "客户端ID", "header"),
                new ApiKey("clientSecret", "客户端秘钥", "header"),
                new ApiKey("accessToken", "客户端访问标识", "header"));
    }

    private List<SecurityContext> securityContexts() {
        return newArrayList(
                SecurityContext.builder()
                        .securityReferences(defaultAuth())
                        .forPaths(PathSelectors.regex("/*.*"))
                        .build()
        );
    }

    List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        return newArrayList(
                new SecurityReference("clientId", authorizationScopes),
                new SecurityReference("clientSecret", authorizationScopes),
                new SecurityReference("accessToken", authorizationScopes));
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring 中使用Swagger2构建RESTful API")
                .termsOfServiceUrl("http://blog.csdn.net/yangshijin1988")
                .description("此API提供接口调用")
                .license("License Version 2.0")
                .licenseUrl("http://blog.csdn.net/yangshijin1988")
                .version("2.0").build();
    }
}

3.Controller中使用注解添加API文档

package com.custom.web.index.controller;

importcom.custom.web.index.entity.Person;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by Administrator on 2017/4/8.
 */
@Controller
@RequestMapping("/person")
@Api(tags="个人业务")
public class PersonController {

    @RequestMapping(value="/getPerson",method= RequestMethod.GET)
    @ApiOperation(httpMethod = "GET", value = "个人信息", produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody Person getPersons() {
        Person person = new Person();
        person.setFirstName("fname");
        person.setLastName("lname");
        person.setAge(37);
        person.setDeptName("dept");
        return person;
    }
}

4.web.xml配置说明

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:/spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/v2/api-docs</url-pattern>
</servlet-mapping>
说明:Springmvc前端控制器扫描路径增加“/v2/api-docs”,用于扫描Swagger的 /v2/api-docs,否则 /v2/api-docs无法生效。
 
5.spring-mvc.xml 增加自动扫描Swagger
<!-- 使用 Swagger Restful API文档时,添加此注解 -->
<mvc:default-servlet-handler />
<mvc:annotation-driven/>
或者
<mvc:resources location="classpath:/META-INF/resources/" mapping="swagger-ui.html"/>
<mvc:resources location="classpath:/META-INF/resources/webjars/" mapping="/webjars/**"/>
 

6.效果展示

Restful API 访问路径:  

http://IP:port/{context-path}/swagger-ui.html  

 
 
 
7、使用Swagger UI模板
可在Swagger官网下载。地址:https://github.com/swagger-api/swagger-ui。
下载完成后将swagger-ui下的dist目录下的模板放入项目中,如在项目web-app下新建swagger放swagger-ui模板。
在spring-mvc中配置swagger文件夹自动过滤。
<mvc:resources mapping="/swagger/**" location="/swagger/" cache-period="31556926" />
将index.html或swagger-ui.html文件js中的url换成url ="/v2/api-docs?group=business-api";
 
SSM三大框架整合Springfox(Swagger2)步骤以及遇到的一些问题 需求是在已有的Spring+SpringMVC+Mybatis的项目中整合Springfox(Swagger2)。在网上查看了非常多的博客,均没有解决问题。经过一天多的摸索,总算是成功的完成了整合。现在将具体的步骤记录下来,希望能给有需求的朋友提供参考。注意:此篇文章仅讲解整合完成SSM三大框架以后整合Springfox(Swagger2) 阅读详情

相关推荐

SpringBoot整合Springfox-Swagger2

SpringBoot整合Springfox-Swagger2 用于前后端接口联调,前端可以看到接口的详细信息,示例如下: 配置代码如下: application.yml文件配置 #enable swagger swagger: enable: true package com.ifly.jkoala.config; import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j; import com.ifly.jkoala.comm

专搞技术的小兔子 786

【Spring】使用Springfox-Swagger2

@ApiOperation的httpMethod/consumes/produces属性可以不指定,会自动获取SpringMVC的@RequestMapping中的method/consumes/produces属性值

qq_27579471的博客 1万+

Megatron单机多卡Qwen2.5:pre-train+sft

英伟达:https://github.com/NVIDIA/Megatron-LM/releases/tag/core_r0.8.0阿里:https://github.com/alibaba/Pai-Megatron-Patch/releases/tag/v0.10.2。

weixin_46873015的博客 2186

Springfox-swagger2集成SpringBoot2.7.x

Swagger2是一个用于设计、构建和文档化Web API的工具。它允许开发人员定义API的结构、请求和响应格式,并生成交互式的API文档。开发人员在类上使用注解描述接口,Swagger直接在网页上生成和展示API,使得API的开发和维护更加简单和高效。

baomingshu的博客 2003

SolidWorks + AI:利用 DeepSeek AI 自动生成 VBA Macro 宏,实现智能化建模!

本教程介绍如何将集成到插件,实现的自动化操作。通过 DeepSeek AI,您可以快速生成高效的,提升!💡。

SolidWorksAPI的专栏 1万+

YOLOv11涨点改进 | 全网创新首发、主干改进篇 | CVPR 2025 | RAVLT主干让 YOLOv11 更加强大!通过增强线性注意力机制,特别在小目标和复杂背景的检测上表现突出

本文提出RAVLT主干网络,通过改进YOLOv11的空间特征建模能力,显著提升小物体和复杂背景检测性能。RAVLT采用秩增强线性注意力(RALA)机制,在保持线性计算复杂度的同时解决传统线性注意力低秩问题。RALA通过增强KV缓冲区和输出特征矩阵的秩,提高特征多样性和建模能力。实验表明,RAVLT在ImageNet-1k上达到84.4% Top-1准确率,仅需2600万参数和4.6GFLOPs,性能优于现有线性注意力模型。文章详细介绍了RAVLT模块结构、实现原理及在YOLOv11中的集成方法,包含四种主干

Ai缝合怪、助力小伙伴高效发论文 641

springboot2.7接入springfox-swagger2遇到的问题,以及解决方案

解决springboot2.7 接入springfox-swagger2遇到的问题

zxl_leo的专栏 1万+

CS之攻击菜单详解-后门生成与上线

能够生成如HTA文档、office宏、payload生成器、exe程序。 windows可执行程序exe 可以看到能生成两种windows可执行程序,其分别代表stage分阶段和unstage(stageless)不分阶段。传送门 ——>Cobalt Strike中payload的stage与unstage区别 选择监听器后,按需生成不同类型的exe 然后在目标机器运行即可上线 payload生成器 选择生成如下类型的后门 ——>https://che...

渗透之路,攻防之间皆学问 6811

Swagger2详细教程

swagger的快速入门及详细注解说明

m0_71239320的博客 3031

C# NX二次开发-查找连续倒圆角面

可以通过ufun函数 UF_MODL_ask_face_type 和 UF_MODL_ask_face_props 可判断处理选择相应的一组圆角面。在QQ群里有人问怎么通过一个选择一个倒圆角面来自动选中一组倒圆角面。这个是初步思路,如果有更好的思路可回复讨论一下。

fhqiwmc的博客 624

15、运行语音识别应用:从Mac到微控制器的部署指南

本文详细介绍了如何在Mac上运行语音识别应用,并将其部署到Arduino Nano 33 BLE Sense和SparkFun Edge等微控制器设备上的完整流程。内容涵盖应用构建、设备特定的音频与输出实现、部署步骤、常见问题解决方案及进一步优化建议。通过本指南,开发者可快速实现基于TensorFlow Lite的嵌入式语音识别系统,并根据需求进行自定义修改与功能扩展。

l3m4n的博客 62

​Springfox swagger2 源码解析

​Springfox swagger2 源码解析 doc访问页面http://localhost:8080/doc.html api-json访问页面http://localhost:8080/v2/api-docs pom依赖 <!-- https://mvnrepository.com/artifact/com.github.xiaoymin/knife4j-spring-boot-starter --><dependency> <groupId&gt...

lvzhyt的专栏 1806

用生成式AI自动生成测试用例的完整教程

本文旨在帮助测试工程师、开发人员快速掌握“生成式AI自动生成测试用例”的核心技术,覆盖从概念理解、工具选择到实战落地的全流程。无论是Web应用、API接口还是移动端功能测试,你都能找到可复用的方法。本文将按照“概念→原理→实战→避坑”的逻辑展开:先通过生活案例理解生成式AI如何生成测试用例,再拆解核心算法原理,接着用Python代码演示完整生成流程,最后分享实际应用中的优化技巧和未来趋势。生成式AI:能“创造内容”的AI模型(如ChatGPT、CodeLlama),像会写作文的智能笔。测试用例。

AIGC应用创新大全的博客 1588

Springfox-Swagger2

1、引入包 在https://mvnrepository.com仓库里搜springfox可以找到下面两个包 扫描程序生成文档数据 <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <ver...

qq_30436011的博客 538

华为OD机试真题 新系统 2026-08-02 C++ 实现【战士技能规划设计】

你为一款动作游戏设计战士角色的技能。战士每个技能会消耗不同能量,释放技能有两个约束:连续释放技能数量不能超过 m 个,技能能量总和不能超过能量上限 k;如果超过则必须中断当前技能,进入调息状态,也就是分段。

MISAYAONE的博客 80

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

springfox-swagger2坑

环境:jdk1.8+,spring boot(2.1.3.RELEASE),springfox-swagger2(2.5.0),springfox-swagger-ui(2.5.0) 最近使用springfox-swagger2。本来好好的接口,有一天突然发现swagger显示的接口参数A跟原来不一致,接口参数的描述变成了另外一个类A1的描述。检查后发现接口没变。但是别的同事增加了另外一个接口,...

wqlpz23045的博客 3950

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

springfox-swagger2

springfox-swagger2 文档:http://springfox.github.io/springfox/docs/current/#customizing-the-swagger-endpoints 不说别的, 2.9.0的UI特别帅,所以还是用2.9.0的版本 maven &lt;dependency&gt; &lt;groupId&gt;io.springfo...

www.yiye.tech 3333

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

使用springfox+swagger2书写API文档

主要介绍springfox自动生成API文档的配置与使用并结合swagger2展示书写的API,着重介绍springMVC中如何使用配置、使用springfox。

_好好学习的博客 5万+

springfox-swagger2 3.0.0使用教程

最近 SpringFox 3.0.0 发布了,距离上一次大版本2.9.2足足有2年多时间了。可能看到这个名字,很多读者会有点陌生。但是,只要给大家看一下这两个依赖,你就知道了! <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>3.0.0</version&g

fygkchina的专栏 1万+

使用Springfox的Spring App的Swagger-2 API文档

1. Overview Spring&#26159;&#21019;&#24314;Web&#24212;&#29992;&#31243;&#24207;的非&#24120;&#27969;&#34892;的JEE&#26694;&#26550;&#12290; 通&#24120;&#65292;&#...

专业的开发者“讨论” 337

SpringBoot集成Springfox 3.0使用Swagger2.0生成

SpringBoot集成Springfox 3.0 Springfox介绍 Swagger Swagger是一套用于RESTful API开发的工具,可以生成非常直观的接口文档用于前后端分离的接口交流。 Springfox SpringFox是一个扫描代码中的信息,生成API文档的工具。API文档的格式不止swagger的OpenAPI规范,还有RAML、jsonAPI等。 Springfox官方文档 使用Springfox Maven 引入最新版的Springfox依赖 <repositories

羽轻浮世殇 1271

springfox-swagger2版本和jdk版本

springfox-swagger2版本和jdk版本 springfox-swagger2版本 swagger-models版本 jdk版本 发布时间 2.0.3 1.5.0 1.7.0_60 2015 2.1.2 1.5.0 1.7.0_60 – – – 2.2.2 1.5.03 1.8.0_45 2015 2.3.1 1.5.5 1.8.0_45 2016 ...

【轰炸机】的博客 4908

SpringMVC+Springfox-Swagger2动态生成Restful API文档

Maven + Spring MVC +SpringFox-Swagger2 的整合与配置文档,主要是进行理论操作,已经测试成功并使用!如有问题和沟通可以留言交流,对您的帮助就是对我最大的肯定!

Wyz1960747127的博客 867

springfox-swagger2 与 swagger 的区别

springfox 的官方文档 http://springfox.github.io/springfox/docs/current/ 简单说就是, Swagger 是一种规范。 springfox-swagger2 是基于 Spring 生态系统的该规范的实现。 springfox-swagger-ui 是对 swagger-ui 的封装,使其可以使用 Spring 的服务。 ...

ayudnl的专栏 4652

Springfox Swagger2 vs Springdoc OpenAPI

Springfox Swagger2 vs Springdoc OpenAPI

weixin_45428910的博客 1049
上一篇: 微服务之Swagger
下一篇: springmvc与swagger的集成
hxpjava1
博客等级 码龄19年 1365粉丝 511原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址