liujie
2026-01-24 13392606c16362d23478e84e0d43e869813d3095
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.estate.expert.admin.config;
 
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
 
/**
 * Swagger配置类
 */
@Configuration
@EnableOpenApi // 使用OpenApi注解而不是Swagger2注解
public class SwaggerConfig implements WebMvcConfigurer {
 
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.OAS_30) // 使用OAS_30而不是SWAGGER_2
                .apiInfo(apiInfo())
                .select()
                // 扫描所有需要生成API文档的包路径
                .apis(RequestHandlerSelectors.basePackage("com.estate.expert.admin.controller"))
                .paths(PathSelectors.any())
                .build();
    }
 
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("地产专家管理平台API文档")
                .description("地产专家管理平台的所有RESTful API接口文档")
                .version("1.0.0")
                .build();
    }
 
    // 配置静态资源映射,确保Swagger UI资源可以正确访问
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // Springfox 3.0.0 Swagger UI资源映射
        registry.addResourceHandler("/swagger-ui/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/");
    }
}