Search K
Appearance
Appearance
<!-- pom.xml -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
如果没有config文件夹可以手动建立
// java/config/SwaggerConfig.java
package com.fmmall.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 //开启swagger
public class SwaggerConfig {
@Bean
public Docket createSwaggerDocket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(AipInfo()) //api文档的描述信息
.select()
.apis(RequestHandlerSelectors.basePackage("com.fmmall.api")) //需要扫描的controller包路径
.paths(PathSelectors.any())
.build();
}
private ApiInfo AipInfo() {
return new ApiInfoBuilder()
.title("描述")
.description("信息")
.version("v1.0.0")
.build();
}
}
WARNING
如果配置后访问/swagger-ui.html报404,则添加如下配置
// java/config/WebMvcConfigurer.java
package com.fmmall.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration
public class WebMvcConfigurer extends WebMvcConfigurationSupport {
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
// 解决静态资源无法访问
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/static/");
// 解决swagger无法访问
registry.addResourceHandler("/swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
// 解决swagger的js文件无法访问
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
配置完成后,由于我配置的端口是8080
,只需要访问locolhost:8080/swagger-ui.html
即可