SpringMVC集成Swagger2,配置包含后缀的URL

目录

1、引入Swagger2依赖

2、配置spring

3、新增Swagger配置类

4、添加测试controller

5、配置web.xml

6、运行效果


 

1、引入Swagger2依赖


   2.9.2

	


	io.springfox
	springfox-swagger2
	${version.swagger}


	io.springfox
	springfox-swagger-ui
	${version.swagger}


	com.github.xiaoymin
	swagger-bootstrap-ui
	1.9.2

2、配置spring

在主spring.xml中引入spring-swagger.xml

spring-swagger.xml如下




    
    
    
    
    


 

3、新增Swagger配置类

package com.vizhuo.swagger.config;

/**
 * @Author: maq
 * @Date: 2019/4/19
 */

import io.swagger.annotations.ApiOperation;
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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableWebMvc
@Configuration
@EnableSwagger2
@ComponentScan(basePackages = "com.vizhuo.*.controller")
public class SwaggerConfiguration {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //.apis(RequestHandlerSelectors.basePackage("com.vizhuo"))
//                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("使用Swagger2构建RESTful APIs")
                .description("相关文章请关注:https://github.com/swagger-api/swagger-ui")
                .termsOfServiceUrl("http://localhost:8081/swagger-ui.html")
                .contact("maq")
                .version("1.0")
                .build();
    }

}
 
package com.vizhuo.swagger.config;

import io.swagger.models.Path;
import io.swagger.models.Swagger;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 将接口url中追加模式后缀.do
 *
 * @author impler
 * @date 2017年9月30日
 */
@Aspect
@EnableAspectJAutoProxy
@Component
public class SwaggerApiSuffixAspect {

    @AfterReturning(pointcut = "execution(public io.swagger.models.Swagger springfox.documentation.swagger2.mappers.ServiceModelToSwagger2MapperImpl.mapDocumentation(..))",
            returning = "swagger")
    public void doBeforeBussinessCheck(Swagger swagger) {
        Map paths = swagger.getPaths();
        if (null != paths) {
            Map newPaths = new HashMap(paths);
            paths.clear();
            Iterator it = newPaths.keySet().iterator();
            while (it.hasNext()) {
                String oldKey = it.next();
                String newKey = oldKey + ".do";
                paths.put(newKey, newPaths.get(oldKey));
            }
//            swagger.setPaths(paths);
            newPaths = null;
        }
    }


}

 

4、添加测试controller


/**
 * @Author: maq
 * @Date: 2019/4/19
 */

import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import io.swagger.annotations.Api;


@Controller
@RequestMapping(value = "/TestExcelFast")
@Api(value = "restful", description = "测试")
public class TestExcelFastController {

    @ApiOperation(value = "测试专用")
    @RequestMapping(value = "/test", method = RequestMethod.GET)
    @ResponseBody
    public String test() {
        String str = "123";
        System.out.println(1);
        return str;
    }
}


5、配置web.xml


        SpringMVC
        *.do
        
        /v2/api-docs
        /v2/api-docs-ext
        /swagger-resources
        /swagger-resources/configuration/security
        /swagger-resources/configuration/ui
    

 

6、运行效果

SpringMVC集成Swagger2,配置包含后缀的URL_第1张图片

 

 

SpringMVC集成Swagger2,配置包含后缀的URL_第2张图片

 

参考资料:https://github.com/Impler/SwaggerIntegration?tdsourcetag=s_pcqq_aiomsg

 

 

 

 

 

你可能感兴趣的:(【Java】,swagger2)