1.导入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();
}
/**
* 此处内容用来配置文档信息
* @return
*/
private ApiInfo apiInfo() {
ApiInfo apiInfo = new ApiInfoBuilder()
.title("未来科技掌门人")
.description("王老菊的修炼功法")
.version("V1.0.0")
.build();
return apiInfo;
}
}
3.在webapp 导入swagger ui (下载地址:https://github.com/swagger-api/swagger-ui 注意只需要dist里面的内容)
image4.在springmvc 中配置静态资源放行
<mvc:default-servlet-handler/>
5.启动项目
输入:http://localhost:8080/swagger-ui.html#/
网友评论