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:
- 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> <ARTIFAC Tid>spring-core</artifactid> </exclusion> <exclusion> <groupId>org.springframework</groupId> <ARTIFACTID>SPRING-BEANS</ARTIFAC tid> </exclusion> <exclusion> <GROUPID>ORG.SPR Ingframework</groupid> <artifactId>spring-context</artifactId> < ;/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< ;/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> </EXCLUSION&G T <exclusion> <groupId>org.springframework</groupId> <ARTIFAC tid>spring-oxm</artifactid> </exclusion> </exclusions> </de pendency> <dependency> <groupId>io.springfox</groupId> <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.
- 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 (); } }
- 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/**"/>
- 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