1.導(dǎo)入jar包
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
2.編寫swagger配置文件
package com.jjzy.document;
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;
@Configuration
@EnableSwagger2
@EnableWebMvc
@ComponentScan("com.jjzy.controller")
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.jjzy.controller"))
.paths(PathSelectors.any())
.build();
}
/**
* 此處內(nèi)容用來配置文檔信息
* @return
*/
private ApiInfo apiInfo() {
ApiInfo apiInfo = new ApiInfoBuilder()
.title("未來科技掌門人")
.description("王老菊的修煉功法")
.version("V1.0.0")
.build();
return apiInfo;
}
}
3.在webapp 導(dǎo)入swagger ui (下載地址:https://github.com/swagger-api/swagger-ui 注意只需要dist里面的內(nèi)容)
4.在springmvc 中配置靜態(tài)資源放行
<mvc:default-servlet-handler/>
5.啟動(dòng)項(xiàng)目
輸入:http://localhost:8080/swagger-ui.html#/