Spring 4.2. More than 2 versions and swagger integration schemes and stepped pits

Source: Internet
Author: User

Because the company uses the spring version is too high, in the integration of swagger, there will be some problems, and many of the online instances are mostly version of the lower, in order to be friends less than the pit, my side will integrate the process to record:

    1. Introduce spring, swagger related jar packages (Springfox-swagger2, Springfox-swagger-ui), CONFIGURED in Pom.xml:
<dependency> <groupId>io.springfox</groupId> <artifactid>springfox-swagg                  Er2</artifactid> <version>2.4.0</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> &LT;ARTIFAC                      Tid>spring-core</artifactid> </exclusion> <exclusion> <groupId>org.springframework</groupId> &LT;ARTIFACTID&GT;SPRING-BEANS&LT;/ARTIFAC tid> </exclusion> <exclusion> &LT;GROUPID&GT;ORG.SPR Ingframework</groupid> <artifactId>spring-context</artifactId> &lt                       ;/exclusion> <exclusion> <groupId>org.springframework</groupId> &lT;artifactid>spring-context-support</artifactid> </exclusion> <exclusi On> <groupId>org.springframework</groupId> <artifactid>spri Ng-aop</artifactid> </exclusion> <exclusion> <                  Groupid>org.springframework</groupid> <artifactId>spring-tx</artifactId> </exclusion> <exclusion> <groupid>org.springframework&lt                  ;/groupid> <artifactId>spring-orm</artifactId> </exclusion>                      <exclusion> <groupId>org.springframework</groupId>                       <artifactId>spring-jdbc</artifactId> </exclusion> <exclusion> &lT;groupid>org.springframework</groupid> <artifactId>spring-web</artifactId> </exclusion> <exclusion> <groupid>org.springframework </groupId> <artifactId>spring-webmvc</artifactId> &LT;/EXCLUSION&G                  T <exclusion> <groupId>org.springframework</groupId> &LT;ARTIFAC tid>spring-oxm</artifactid> </exclusion> </exclusions> </de pendency> <dependency> <groupId>io.springfox</groupId> &LT;ARTIFAC Tid>springfox-swagger-ui</artifactid> <version>2.4.0</version> </dependency&   Gt

Note: In particular, SPRINGFOX-SWAGGER2 has introduced spring-related jars in the integration, especially Spring-context, spring-context-support versions, and the versions used in the project are completely inconsistent. The project started with a lot of packet conflict issues, this way when the introduction of the Pom.xml file to filter out the spring related jar package, such as the green flag.

    1. To write the Swagger configuration class:
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 Swa                  Ggerconfig {@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 ("Interface List v1.0") . Description ("interface Information"). Termsofserviceurl ("http://honghu.com"). Contact (""      , "", "Honghu"). Version ("1.1.0"). Build ();   }  }
    1. Configure the filter in the Spring-mvc.xml file to filter out the associated access configuration for swagger:
<mvc:exclude-mapping path="/swagger*/**"/>  <mvc:exclude-mapping path="/v2/**"/>  <mvc:exclude-mapping path="/webjars/**"/>  
    1. Service Configuration Items
<span style= "color: #ff0000;" > @Api ("Regional Services") </span> @RestController @RequestMapping (value = "/rest/area") public class Areaservice {@A      utowired private Areaservice Areaservice; <span style= "color: #ff0000;" > @ApiOperation (value = "Area List", HttpMethod = "GET", notes = "area List") </span> @IsLogin @ResponseBody @               Requestmapping (value = "Treedata", method = requestmethod.get) public list<map<string, Object>> Treedata ( <span style= "color: #ff0000;" > @ApiParam (required = true, value = "Area 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;   }  }

Start the project to see the results:


This is the end!

Spring 4.2. More than 2 versions and swagger integration schemes and stepped pits

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.