1.导入mavan相关资源
<!-- swagger-springmvc dependencies -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.9</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.9</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.9</version>
</dependency>
2.编写SwaggerConfig类,注意包名换成自己的
package com.sansence.wine.swagger;
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 org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
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;
@Configuration // 配置注解,自动在本类上下文加载一些环境变量信息
@EnableSwagger2 // 使swagger2生效
@EnableWebMvc
@ComponentScan(basePackages = {"com.sansence.wine"}) //需要扫描的包路径
public class SwaggerConfig extends WebMvcConfigurationSupport {
@Bean
public Docket swaggerSpringMvcPlugin() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("business-api")
.select() // 选择那些路径和api会生成document
.apis(RequestHandlerSelectors.basePackage("com.sansence.wine"))
.paths(PathSelectors.any())
//.apis(RequestHandlerSelectors.any()) // 对所有api进行监控
//.paths(PathSelectors.any()) // 对所有路径进行监控
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Spring 中使用Swagger2构建RESTful API")
.termsOfServiceUrl("http://blog.csdn.net/yangshijin1988")
.description("此API提供接口调用")
.license("License Version 2.0")
.licenseUrl("http://blog.csdn.net/yangshijin1988")
.version("2.0").build();
}
}
3.将上面的类注入springmvc
<bean class="com.sansence.wine.swagger.SwaggerConfig" />
4.开启注解
<!-- 开启注解 -->
<mvc:annotation-driven/>
<mvc:default-servlet-handler />
5.web.xml加入该代码
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/v2/api-docs</url-pattern>
</servlet-mapping>
6、使用Swagger UI模板
可在Swagger官网下载。地址:https://github.com/swagger-api/swagger-ui。
下载完成后将swagger-ui下的dist目录下的模板放入项目中,如在项目web-app下新建swagger放swagger-ui模板。
在spring-mvc中配置swagger文件夹自动过滤。
<mvc:resources mapping="/swagger/**" location="/swagger/" cache-period="31556926" />
将index.html或swagger-ui.html文件js中的url换成url ="http://localhost:8080/project/v2/api-docs?group=business-api";
网友评论