1.引入依赖
<!--swagger start-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<!--1.通过 http://${host}:${port}/swagger-ui.html 来访问-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
<!--2.swagger-bootstrap-ui默认访问地址是:http://${host}:${port}/doc.html-->
<!--Swagger-Bootstrap-UI地址:https://github.com/xiaoymin/Swagger-Bootstrap-UI-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.6</version>
</dependency>
<!--swagger end-->
2.配置swagger
package com.hp.backend.config.swagger;
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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* @author haopeng
* @date 2018-08-07 10:44
*/
@Configuration
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.hp.backend.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("boot-backend API文档")
.termsOfServiceUrl("http://localhost/8888")
.version("1.0")
.contact(new Contact("your name", "http://www.baidu.com", "180******@163.com"))
.description("基于swagger构建的在线API文档")
.build();
}
}
3.配置静态资源映射
由于springboot默认静态资源文件与swagger生成的文档目录不对应所以直接访问会出现404 Not Find ,解决办法就是配置静态资源路径,使swagger为我们生成的文档可以被访问。
package com.hp.backend.config.web;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
/**
* @author haopeng
* @date 2018-08-07 12:53
*/
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
/**
* 发现如果继承了WebMvcConfigurationSupport,则在yml中配置的相关内容会失效。
* 需要重新指定静态资源
* @param registry
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/static/");
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("doc.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
super.addResourceHandlers(registry);
}
/**
* 配置servlet处理
*/
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
4.enjoy it
- 浏览器输入:
http://localhost:8888/swagger-ui.htm 或者
http://localhost:8888/doc.html
可以实时在线查看并调试接口文档!!!
网友评论