项目升级SpringBoot3:java.lang.ClassNotFoundException: javax.servlet.http.HttpServletRequest

1、报错信息

Caused by: java.lang.ClassNotFoundException: javax.servlet.http.HttpServletRequest
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641) ~[na:na]
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) ~[na:na]
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520) ~[na:na]
	at java.base/java.lang.Class.forName0(Native Method) ~[na:na]
	at java.base/java.lang.Class.forName(Class.java:467) ~[na:na]
	at java.base/sun.reflect.generics.factory.CoreReflectionFactory.makeNamedType(CoreReflectionFactory.java:114) ~[na:na]
	... 32 common frames omitted

2、报错原因

由于在SpringBoot3版本中将javax.servlet.http.HttpServletRequest更换为jakarta.servlet.http.HttpServletRequest,如果我们项目中已经将所有的javax包改成了jakarta,并且项目中使用了Swagger,那么应该是Swagger版本问题,SpringBoot3中需要将springfox替换为springdoc。

3、报错解决

我这里使用的是knife4j来增强Swagger。

① Maven

<dependency>
    <groupId>com.github.xiaoymingroupId>
    <artifactId>knife4j-openapi3-jakarta-spring-boot-starterartifactId>
    <version>4.1.0version>
dependency>
<dependency>
    <groupId>org.projectlombokgroupId>
    <artifactId>lombokartifactId>
    <optional>trueoptional>
dependency>

② 配置类

@Configuration
public class SwaggerConfig implements WebMvcConfigurer {
    
    @Value("${spring.application.name:我的应用}")
    private String applicationName;

    @Bean
    public OpenAPI springShopOpenAPI() {
        return new OpenAPI()
                .components(new Components()
                        .addParameters("token",new HeaderParameter().description("请填写Token").schema(new StringSchema()))
                        .addParameters("adminID",new HeaderParameter().description("请填写用户ID").schema(new StringSchema())))
                .info(new Info().title(applicationName)
                        .description("Knife4j增强文档")
                        .version("v2.0")
                        .license(new License().name("Apache 2.0").url("http://springdoc.org")))
                .externalDocs(new ExternalDocumentation()
                        .description("二饭快速开发框架")
                        .url("https://gitee.com/StandFast"));
    }
}

③ 配置文件

knife4j:
  enable: true
  setting:
    language: zh_cn
springdoc:
  api-docs:
    path: /v3/api-docs
  group-configs:
    - group: 'default'
      paths-to-match: '/**'
      packages-to-scan: cn.tworice

你可能感兴趣的:(SpringBoot,Web错误集锦,java,spring,boot)