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 the 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>or G.springframework</groupid> <artifactId>spring-core</artifactId> & Lt;/exclusion> <exclusion> &LT;GROUPID&GT;ORG.SPRINGFRAMEWORK&LT;/GROUPID&G                      T                      <artifactId>spring-beans</artifactId> </exclusion> <exclusion> <groupId>org.springframework</groupId> <artifactid>spring-conte xt</artifactid> </exclusion> <exclusion> <grou Pid>org.springframework</groupid> <artifactid>spring-context-support</artifactid&gt                  ; </exclusion> <exclusion> <groupid>org.springfraMework</groupid> <artifactId>spring-aop</artifactId> </exclusio                      N> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> </exclusion> <exclus Ion> <groupId>org.springframework</groupId> &LT;ARTIFACTID&GT;SPR ing-orm</artifactid> </exclusion> <exclusion> &lt                  ;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</arti Factid> <version>2.4.0</version> </dependency>

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.

2. 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 classSwaggerconfig {@Bean PublicDocket Createrestapi () {return NewDocket (documentationtype.swagger_2). Apiinfo (Apiinfo ()).Select(). APIs (Requesthandlerselectors.basepackage ("Com.ml.honghu"). Paths (Pathselectors.any ()). build (); }        Privateapiinfo Apiinfo () {return NewApiinfobuilder (). Title ("Interface List v1.0"). Description ("Interface Information"). Termsofserviceurl ("http://honghu.com"). Contact (NewContact ("","","Honghu"). Version ("1.1.0"). build (); }  }  

3. 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/**"/>  

4. Service Configuration Items

<span style="color: #ff0000;"> @Api ("Regional Services") </span>@RestController @RequestMapping (value="/rest/area")   Public classAreaservice {@AutowiredPrivateAreaservice Areaservice; <span style="color: #ff0000;"> @ApiOperation (value ="Area list", HttpMethod ="GET", notes ="Area list") </span>@IsLogin @ResponseBody @RequestMapping (value="Treedata", method =requestmethod.get) PublicList<map<string, object>>Treedata (<span style="color: #ff0000;"> @ApiParam (required =true, value ="Zone ID") </span> @RequestParam (required=false) String Extid, httpservletresponse response) {List<map<string, object>> maplist =lists.newarraylist (); List<Area> list =Areaservice.findall ();  for(intI=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); }          }          returnmaplist; }  }  

Start the project to see the results:

By this 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.