Spring 4.2.2 and above and swagger integration solutions and pits that have been stepped on

Because the spring version used by the company is too high, there will be some problems when integrating swagger, and most of the online instances are of relatively low version. In order to make friends less, I will record the integration process here:

1. Introduce spring and swagger related jar packages (springfox-swagger2, springfox-swagger-ui) and configure them in 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>

 

 

Reminder: Pay special attention, when springfox-swagger2 is integrated, related jars of spring have been introduced, especially the versions of spring-context and spring-context-support are completely inconsistent with the versions used in the project, and many projects appear at startup For the problem of package conflict, when introducing the pom.xml file, the related jar packages of spring are filtered out, 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 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("Interface List v1.0")
                .description("Interface Information")
                .termsOfServiceUrl("http://honghu.com")
                .contact(new Contact("", "", "HongHu"))
                .version("1.1.0")
                .build();
    }
}

 Reminder: Pay attention to the places marked in red

 

 

3. Configure the filter in the spring-mvc.xml file to filter out the related access configuration of swagger:

 

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

 

 

4. Service configuration items

 

@Api("region service")
@RestController
@RequestMapping(value = "/rest/area")
public class AreaService  {

	@Autowired
	private AreaService areaService;
	
	@ApiOperation(value = "region list", httpMethod = "GET", notes = "region list")
	@IsLogin
	@ResponseBody
	@RequestMapping(value = "treeData", method = RequestMethod.GET)
	public List<Map<String, Object>> treeData(
			@ApiParam(required = true, value = "区域ID")  @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;
	}
}

 

 

4. Start the project and view the results:



 

It ends here! !

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326227518&siteId=291194637