1、首先,创建一个Maven工程

3、然后创建两个module,分别作为web程序入口和配置swagger的模块。

5、现在父pom文件里已经指定了这两个子模块,子模块中也指定了父pom:

6、既然boot是作为程序入口,所以它的打包方式为war即可。

12、其中第一条是指开启注解功能,第二条注册了一个bean实例,用来指定容器初始化后重定向的页面信息;第三条声明了一个类路径下的扫描器;第四条用于引入swagger的配置文件context-swagger.xml。RedirectCtr类如下:

14、首先,它注册了一个bean,用于配置swagger的基本信息,然后指定了swagger的页面(后面几个不能随意改变)。SwaggerConfig配置类如下:package com.swagger.test.common;import com.google.common.base.Predicate;import org.springframework.context.annotation.Bean;import springfox.documentation.builders.ApiInfoBuilder;import springfox.documentation.service.ApiInfo;import springfox.documentation.spi.DocumentationType;import springfox.documentation.spring.web.plugins.Docket;import springfox.documentation.swagger2.annotations.EnableSwagger2;import static com.google.common.base.Predicates.or;import static springfox.documentation.builders.PathSelectors.regex;/*** @author tian.luye*/@EnableSwagger2public class SwaggerConfig { @Bean public Docket selfCareApi() { return new Docket(DocumentationType.SWAGGER_2) .groupName("swagger") .apiInfo(selfCareApiInfo()) .select() .paths(selfCareApiPaths()) .build(); } private ApiInfo selfCareApiInfo() { return new ApiInfoBuilder() .title("Test Swagger API") .version("1.0-SNAPSHOT") .build(); } private Predicate<String> selfCareApiPaths() { //noinspection unchecked return or( regex("/test.*") ); }}
15、这个配置类描述的非常清楚,只配置了两处信息:一是Swagger的基本描述信息,二是Swagger的拦截路径,即加在类上的RequestMapping的path值。最后,我们需要编写Controller接口代码
