iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >Spring 4.2.2以上版本和swagger集成方案和踩过的坑
  • 463
分享到

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

2023-06-05 04:06:09 463人浏览 薄情痞子
摘要

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

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

引入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包。

编写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();      }  }

在spring-mvc.xml文件中进行过滤器的配置,过滤掉swagger的相关访问配置:

<mvc:exclude-mapping path="/swagger*/**"/>  <mvc:exclude-mapping path="/v2/**"/>  <mvc:exclude-mapping path="/webjars/**"/>

服务配置项

@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;      }  }

启动项目,查看结果:

Spring 4.2.2以上版本和swagger集成方案和踩过的坑资料和源码来源 

--结束END--

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

本文链接: https://www.lsjlt.com/news/241375.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作