Spring 4.2.2以上版本和swagger集成方案和踩过的坑

简介: Spring 4.2.2以上版本和swagger集成方案和踩过的坑

因为公司使用的spring版本太高,在集成swagger的时候会存在一些问题,而网上的很多实例大多都是版本比较低的,为了是朋友们少才坑,我这边将集成的过程记录一下:

<dependency>  
            <groupId>io.springfox</groupId>  
            <artifactId>springfox-swagger2</artifactId>  
            <version>2.4.0</version>  
            <exclusions>  
                <exclusion>  
                    <groupId>org.springframework</groupId>  
                    <artifactId>spring-core</artifactId>  
                </exclusion>  
                <exclusion>  
                    <groupId>org.springframework</groupId>  
                    <artifactId>spring-beans</artifactId>  
                </exclusion>  
                <exclusion>  
                    <groupId>org.springframework</groupId>  
                    <artifactId>spring-context</artifactId>  
                </exclusion>  
                <exclusion>  
                    <groupId>org.springframework</groupId>  
                    <artifactId>spring-context-support</artifactId>  
                </exclusion>  
                <exclusion>  
                    <groupId>org.springframework</groupId>  
                    <artifactId>spring-aop</artifactId>  
                </exclusion>  
                <exclusion>  
                    <groupId>org.springframework</groupId>  
                    <artifactId>spring-tx</artifactId>  
                </exclusion>  
                <exclusion>  
                    <groupId>org.springframework</groupId>  
                    <artifactId>spring-orm</artifactId>  
                </exclusion>  
                <exclusion>  
                    <groupId>org.springframework</groupId>  
                    <artifactId>spring-jdbc</artifactId>  
                </exclusion>  
                <exclusion>  
                    <groupId>org.springframework</groupId>  
                    <artifactId>spring-web</artifactId>  
                </exclusion>  
                <exclusion>  
                    <groupId>org.springframework</groupId>  
                    <artifactId>spring-webmvc</artifactId>  
                </exclusion>  
                <exclusion>  
                    <groupId>org.springframework</groupId>  
                    <artifactId>spring-oxm</artifactId>  
                </exclusion>  
            </exclusions>  
        </dependency>  
        <dependency>  
            <groupId>io.springfox</groupId>  
            <artifactId>springfox-swagger-ui</artifactId>  
            <version>2.4.0</version>  
        </dependency>  

提醒: 特别注意,springfox-swagger2在集成的时候,已经引入了spring的相关jar,特别是spring-context、spring-context-support的版本和项目中使用的版本完全不一致,项目在启动的时候出现很多包冲突的问题,这边在引入pom.xml文件的时候过滤掉了spring的相关jar包,如绿色标志。

  1. 编写Swagger的配置类:
package com.ml.honghu.swagger.web;  
  
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 springfox.documentation.builders.ApiInfoBuilder;  
import springfox.documentation.builders.PathSelectors;  
import springfox.documentation.builders.RequestHandlerSelectors;  
import springfox.documentation.service.ApiInfo;  
import springfox.documentation.service.Contact;  
import springfox.documentation.spi.DocumentationType;  
import springfox.documentation.spring.web.plugins.Docket;  
import springfox.documentation.swagger2.annotations.EnableSwagger2;  
  
@EnableWebMvc  
@EnableSwagger2  
@Configuration  
@ComponentScan(basePackages ={"com.ml.honghu.**.rest"})  
public class SwaggerConfig {  
    @Bean  
    public Docket createRestApi() {  
        return new Docket(DocumentationType.SWAGGER_2)  
                .apiInfo(apiInfo())  
                .select()  
                .apis(RequestHandlerSelectors.basePackage("com.ml.honghu"))  
                .paths(PathSelectors.any())  
                .build();  
    }  
  
    private ApiInfo apiInfo() {  
        return new ApiInfoBuilder()  
                .title("接口列表 v1.0")  
                .description("接口信息")  
                .termsOfServiceUrl("http://honghu.com")  
                .contact(new Contact("", "", "HongHu"))  
                .version("1.1.0")  
                .build();  
    }  
}  

提醒:注意红色标注的地方

  1. 在spring-mvc.xml文件中进行过滤器的配置,过滤掉swagger的相关访问配置:
<mvc:exclude-mapping path="/swagger*/**"/>  
<mvc:exclude-mapping path="/v2/**"/>  
<mvc:exclude-mapping path="/webjars/**"/>  
  1. 服务配置项
<span style="color: #ff0000;">@Api("区域服务")</span>  
@RestController  
@RequestMapping(value = "/rest/area")  
public class AreaService  {  
  
    @Autowired  
    private AreaService areaService;  
      
    <span style="color: #ff0000;">@ApiOperation(value = "区域列表", httpMethod = "GET", notes = "区域列表")</span>  
    @IsLogin  
    @ResponseBody  
    @RequestMapping(value = "treeData", method = RequestMethod.GET)  
    public List<Map<String, Object>> treeData(  
            <span style="color: #ff0000;">@ApiParam(required = true, value = "区域ID")</span>  @RequestParam(required=false) String extId, HttpServletResponse response) {  
        List<Map<String, Object>> mapList = Lists.newArrayList();  
        List<Area> list = areaService.findAll();  
        for (int i=0; i<list.size(); i++){  
            Area e = list.get(i);  
            if (StringUtils.isBlank(extId) || (extId!=null && !extId.equals(e.getId()) && e.getParentIds().indexOf(","+extId+",")==-1)){  
                Map<String, Object> map = Maps.newHashMap();  
                map.put("id", e.getId());  
                map.put("pId", e.getParentId());  
                map.put("name", e.getName());  
                mapList.add(map);  
            }  
        }  
        return mapList;  
    }  
}  
  1. 启动项目,查看结果:

image

到此结束!

目录
相关文章
|
20天前
|
XML Java Nacos
Spring Boot 整合Nacos 版本兼容适配 史上最详细文档
本文介绍SpringBoot整合Nacos的完整流程,涵盖Nacos下载安装、配置中心与服务发现集成、版本兼容性问题及实战配置。重点解决SpringBoot 3.3.0与Nacos版本适配难题,推荐使用Spring Cloud Alibaba方案,并提供项目开源地址供参考学习。
|
20天前
|
机器学习/深度学习 算法 物联网
面向能效和低延迟的语音控制智能家居:离线语音识别与物联网集成方案——论文阅读
本文提出一种面向能效与低延迟的离线语音控制智能家居方案,通过将关键词识别(KWS)集成至终端设备,结合去中心化Mesh网络与CoAP协议,实现本地化语音处理。相较云端方案,系统能耗降低98%,延迟减少75%以上,显著提升响应速度与能源效率,为绿色智能家居提供可行路径。(236字)
155 17
面向能效和低延迟的语音控制智能家居:离线语音识别与物联网集成方案——论文阅读
编解码 算法 vr&ar
153 0
|
2月前
|
自然语言处理 负载均衡 算法
推理速度提升300%:LLaMA4-MoE的FlashAttention-2集成与量化部署方案
本文详解LLaMA4-MoE模型架构与实现全流程,涵盖语料预处理、MoE核心技术、模型搭建、训练优化及推理策略,并提供完整代码与技术文档,助你掌握大模型MoE技术原理与落地实践。
197 5
|
2月前
|
Cloud Native Java API
Java Spring框架技术栈选和最新版本及发展史详解(截至2025年8月)-优雅草卓伊凡
Java Spring框架技术栈选和最新版本及发展史详解(截至2025年8月)-优雅草卓伊凡
374 0
|
3月前
|
缓存 人工智能 监控
MCP资源管理深度实践:动态数据源集成方案
作为一名深耕AI技术领域多年的开发者,我见证了从传统API集成到现代化协议标准的演进历程。今天要和大家分享的MCP(Model Context Protocol)资源管理实践,是我在实际项目中积累的宝贵经验。MCP作为Anthropic推出的革命性AI连接标准,其资源管理机制为我们提供了前所未有的灵活性和扩展性。在过去的几个月里,我深度参与了多个企业级MCP项目的架构设计和实施,从最初的概念验证到生产环境的大规模部署,每一个环节都让我对MCP资源管理有了更深刻的理解。本文将从资源生命周期管理的角度出发,详细探讨文件系统、数据库、API等多种数据源的适配策略,深入分析实时数据更新与缓存的最佳实践
117 0
|
3月前
|
人工智能 安全 API
MCP vs 传统集成方案:REST API、GraphQL、gRPC的终极对比
作为一名长期关注AI技术发展的博主摘星,我深刻感受到了当前AI应用集成领域正在经历的巨大变革。随着Anthropic推出的Model Context Protocol(MCP,模型上下文协议)逐渐成熟,我们不得不重新审视传统的系统集成方案。在过去的几年中,REST API凭借其简单易用的特性成为了Web服务的标准选择,GraphQL以其灵活的数据查询能力赢得了前端开发者的青睐,而gRPC则以其高性能的特点在微服务架构中占据了重要地位。然而,当我们将视角转向AI应用场景时,这些传统方案都暴露出了一些局限性:REST API的静态接口设计难以适应AI模型的动态需求,GraphQL的复杂查询机制在处
252 0
MCP vs 传统集成方案:REST API、GraphQL、gRPC的终极对比
|
3月前
|
JSON API 开发者
Django集成Swagger全指南:两种实用方案详解
本文介绍了在 Django 项目中集成 Swagger 的两种主流方案 —— drf-yasg 和 drf-spectacular,涵盖安装配置、效果展示及高级用法,助力开发者高效构建交互式 API 文档系统,提升前后端协作效率。
169 5
|
4月前
|
存储 Kubernetes 监控
Docker与Kubernetes集成挑战及方案
面对这些挑战,并不存在一键解决方案。如同搭建灌溉系统需要考虑多种因素,集成Docker与Kubernetes也需要深思熟虑的规划、相当的技术知识和不断的调试。只有这样,才能建立起一个稳定、健康、高效的Docker-Kubernetes生态,让你的应用像花园中的植物一样繁荣生长。
219 63