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

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

因为公司使用的spring版本太高,在集成swagger的时候会存在一些问题,而网上的很多实例大多都是版本比较低的,为了使朋友们少踩坑,我这边将集成的过程记录一下:愿意了解源码的朋友直接求求交流分享技术二一四七七七五六三三

  1. 引入spring、swagger的相关jar包(springfox-swagger2、springfox-swagger-ui),在pom.xml中配置:
<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. 服务配置项
@Api("区域服务")
@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;  
    }  
}  
复制代码

启动项目,查看结果:

资料和源码来源

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/101297.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 30分钟看懂经济运行原理_看懂了自然哲学原理

    30分钟看懂经济运行原理_看懂了自然哲学原理这部分内容主要解释一些概念和术语,最好是先理解这部分内容。公钥密码体制分为三个部分,公钥、私钥、加密解密算法,它的加密解密过程如下:加密:通过加密算法和公钥对内容(或者说明文)进行加密,得到密文。

    2022年8月4日
    9
  • 浅谈时间轮算法[通俗易懂]

    浅谈时间轮算法[通俗易懂]时间轮在计算机世界中,只有待解决的问题变得大规模后,算法的价值才能够最大化的体现。时间轮算法可以将插入和删除操作的时间复杂度都降为O(1),在大规模问题下还能够达到非常好的运行效果。如果我们要实现一个定时任务该如何实现呢?最简单的方式就是使用一个任务队列来完成定时任务。具体实现细节下面详细展开。

    2022年9月27日
    2
  • 选用TypeScript开发AngularJS2[通俗易懂]

    选用TypeScript开发AngularJS2

    2022年3月4日
    41
  • 基于ZigBee的工业废气监测系统

    基于ZigBee的工业废气监测系统摘要本文主要对工业现场中排放的工业废气浓度进行检测。并根据国内外气体监测技术的发展现状,提出了基于ZigBee的工业废气监测系统的设计方案。本系统主要由单片机、气体浓度检测模块、模数转换模块、显示模块、按键、蜂鸣器和无线通信模块等组成。本文首先介绍了工业废气检测系统的研究背景意义,同时结合国内外气体检测技术的发展现状,提出了基于ZigBee的工业废气监测…

    2022年4月14日
    61
  • java虚拟机内存分配_深入理解java虚拟机第二版

    java虚拟机内存分配_深入理解java虚拟机第二版深入理解Java虚拟机之对象的内存布局、访问定位

    2022年4月20日
    42
  • 电子元器件采购知识_电子元件购买

    电子元器件采购知识_电子元件购买​本部分内容为“电子元件知识汇总1-封装、电子元件知识汇总2-封装”的扩展,主要侧重于电子元件的品牌以及采购,若需采购厂商参见“电子元件知识汇总3-厂商”,仅供参考。​

    2022年8月24日
    5

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号