引入依赖
<!-- 界面ui的依赖-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!-- swagger 核心的依赖-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
pom.xml配置阿里云maven下载
<repositories>
<repository>
<id>maven-ali</id>
<url>http://maven.aliyun.com/nexus/content/repositories/central</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
</repository>
</repositories>
configuration中配置解决swagger静态页面无法访问,跨域支持
@Override
public 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/");
}
/* *
* @Author qy
* <p>跨域支持 </p>
* @Param [registry]
* @Return void
*/
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("GET", "POST", "DELETE", "PUT", "PATCH", "OPTIONS", "HEAD")
.maxAge(3600 * 24);
}
swagger配置注入
@Configuration
@EnableSwagger2
public class Swagger2 {
@Value("${swagger.scan.path}")
private String swagger2SCanPath;
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
//RequestMapping 处理这个注解
.apis(RequestHandlerSelectors.withMethodAnnotation(RequestMapping.class))
// 为当前包路径
//swagger2SCanPath 扫描的包
.apis(RequestHandlerSelectors.basePackage(swagger2SCanPath)).paths(PathSelectors.any()).build();
}
@Autowired
private Swagger2MetaConfig metaConfig;
// 构建 api文档的详细信息函数,注意这里的注解引用的是哪个
private ApiInfo apiInfo() {
Contact contact = new Contact(metaConfig.getName(), metaConfig.getUrl(), metaConfig.getEmail());
return new ApiInfoBuilder()
// 页面标题
.title(metaConfig.getTitle())
// 版本号
.version(metaConfig.getVersion())
// 联系方式
.contact(String.valueOf(contact))
// 描述
.description(metaConfig.getDescription()).build();
}
}
@Configuration//放在ioc容器中
@PropertySource("classpath:application.properties")//指定路径
@ConfigurationProperties(prefix = "swagger2") // 将属性封装成实体
public class Swagger2MetaConfig {
private String name;
private String url;
private String email;
private String title;
private String version;
private String description;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
application.properties中写入配置
swagger2.name=应用名称
swagger2.version=1.0
swagger2.email=
swagger2.title=
swagger2.description=
swagger2.url=
#swagger扫描的包路径
swagger.scan.path=com.xxx.controller
启动项目访问路径