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-s Wagger2</artifactid> <version>2.4.0</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </exclusion> <exclusion > <groupId>org.springframework</groupId> <ARTIFACTID>SP Ring-beans</artifactid> </exclusion> <exclusion> <groupId>org.springframework</groupId> <artifactid>spring-context</artif actid> </exclusion> <exclusion> <groupid> ;org.springframework</groupid> <artifactId>spring-context-support</artifactId> </ex Clusion> <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> & Lt;groupid>org.springframework</groupid> <artifactId>spring-jdbc</artifactId> </Exclusion> <exclusion> <GROUPID>ORG.SPRINGFRAMEWORK</GROUPID&G T <artifactId>spring-web</artifactId> </exclusion> <exclusion> <groupId>org.springframework</groupId> <artifactid>spring -webmvc</artifactid> </exclusion> <exclusion> <groupId>org.springframework</groupId> <ARTIFACTID>SPRING-OXM</ARTIFACTID&G T </exclusion> </exclusions> </dependency> <dependency> <groupId>io.springfox</groupId> <ARTIFACTID>SPRINGFOX-SWAGGER-UI</ARTIFACTID&G T <version>2.4.0</version> </dependency>
提醒: 特别注意,springfox-swagger2在集成的时候,已经引入了spring的相关jar,特别是spring-context、spring-context-support的版本和项目中使用的版本完全不一致,项目在启动的时候出现很多包冲突的问题,这边在引入
- 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 cl Swaggerconfig {@Bean public Docket Createrestapi () {return new docket (Documentationtype.swagger_2). Apiinfo (Apiinfo ()). Select (). APIs (reques Thandlerselectors.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 (NE W 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 { @Autowired 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>> t Reedata (<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 = map S.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:
To this end!! Source Source
Spring 4.2. More than 2 versions and swagger integration schemes and stepped pits