- 导入依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
- 添加配置
package com.jwj.book.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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
public class SwaggerConfig{
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2) //主要api配置机制初始化为swagger规范2.0
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.jwj.book.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("图书管理系统") // 标题
.description("rest api 文档构建利器") // 描述信息
.termsOfServiceUrl("http://blog.csdn.net/itguangit") //服务网址
.contact("jingwenjuana") // 联系方式
.version("1.0") //版本号
.build();
}
}
3.访问地址
http://localhost:9898/swagger-ui.html#/