【异常处理】解决更新Springboot 3.0后,Swagger-UI 无法正常启动问题

时间:2025-03-21 10:17:10

背景

今天尝试将一个项目升级到Spring 3.0,发现 Swagger-UI无法打开,显示404 ,尝试更换如下swagger-ui到最新版本,也没有效果,访问页面还是一片空白。

 <dependency>
       <groupId></groupId>
       <artifactId>springfox-swagger-ui</artifactId>
       <version>3.0.0</version>
   </dependency>
   <dependency>
       <groupId></groupId>
       <artifactId>springfox-swagger-ui-springmvc</artifactId>
       <version>3.0.0</version>
   </dependency>

解决方案

更新Springboot 3.0后,使用springdoc-openapi 包替换原有的swagger-UI.

        <dependency>
            <groupId></groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId></groupId>
            <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
            <version>2.1.0</version>
        </dependency>
        <dependency>
            <groupId></groupId>
            <artifactId>-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

更在配置文件加入如下配置:

# swagger-ui custom path
springdoc.swagger-ui.path=/swagger-ui.html

详细配置如下

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
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 api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
}
@Slf4j
@RestController
@Api(value = "SMB Operations", description = "Operations related to SMB files")
public class SmbOperationController {

    private final AuthenticationServiceI authenticationServiceI;

    private final SmbOperationServiceI smbOperationServiceI;

    public SmbOperationController(AuthenticationServiceI authenticationServiceI, SmbOperationServiceI smbOperationServiceI) {
        this.authenticationServiceI = authenticationServiceI;
        this.smbOperationServiceI = smbOperationServiceI;
    }

    @Operation(summary = "Upload a file to the server")
    @ApiResponse(responseCode = "200", description = "File uploaded successfully")
    @PostMapping("/smb/upload")
    public ResponseEntity<Object> upload(
            @ApiParam(value = "FileUploadDTO object containing details of the file to be uploaded", required = true)
            @RequestBody FileUploadDTO fileUploadDTO) {
        Map<String, String> result = new HashMap<>();
        boolean isSuccess = false;
        try {
            final Session session = authenticationServiceI.connectAndVerify(fileUploadDTO.getRemoteHost(), fileUploadDTO.getAccount(), fileUploadDTO.getPassword(), fileUploadDTO.getDomain());
            isSuccess = smbOperationServiceI.upload(session, fileUploadDTO);
        } catch (Exception ex) {
            log.error("Error found in upload, parameters:{},err:", fileUploadDTO.toString(), ex);
            result.put("ErrorMsg", ex.getLocalizedMessage());
        } finally {

            result.put("Status", isSuccess ? "Success" : "Failure");
        }
        return new ResponseEntity<>(result, HttpStatus.OK);
    }
 }