Jason Chuang
Jason Chuang

Reputation: 439

Why does spring-boot-3 give javax.servlet.http.HttpServletRequest ClassNotFoundException

When I ran spring-boot-2.7, there is no issue. However when I changed code and tailored it to spring-boot-3.0, there is java.lang.ClassNotFoundException: javax.servlet.http.HttpServletRequest

Could someone help me to download the source code and run on your computer? After run it, and you click "Login using Facebook", you will see the exception. https://github.com/chuangtc/spring-boot-3.0-security-social-login

I tried adding jarkarta servlet api 6.0, but the exception is still there.

Upvotes: 34

Views: 85072

Answers (4)

Salvador Valencia
Salvador Valencia

Reputation: 1400

The answer by Sivvie Lim fixed it for me (using jakarta.servlet vs. javax.servlet).

But when working with Spring Boot, you don't need to add a dependency for javax.servlet, just make sure you have this in your pom file:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Upvotes: 4

Vitaly Chura
Vitaly Chura

Reputation: 860

In my case I had the following dependencies:

implementation "io.springfox:springfox-swagger2"
implementation "io.springfox:springfox-swagger-ui"

Which apparently somehow depended on the old api, because commenting them solved the issue.

Upvotes: 4

Aparna
Aparna

Reputation: 783

You can also try adding new dependencies as follows. With the below dependencies I was able to retain javax.servlet class.

 <dependency>
        <groupId>javax.annotation</groupId>
        <artifactId>javax.annotation-api</artifactId>
        <version>1.3.2</version>
  </dependency> 
  <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.2.12</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
    </dependency> 

Upvotes: 1

Sivvie Lim
Sivvie Lim

Reputation: 1276

You don't have to add jakarta servlet api 6.0 into the pom.xml. According to documentation here, it is already included.

In your code whichever is using javax.servlet.http.HttpServletRequest, you need to change the javax to jakarta.

So the whole line will be something like this: import jakarta.servlet.http.HttpServletRequest

I faced this error before when I upgrade my service as well and I solved it using this way. Maybe you can try it.

[EDIT]

I see that one of your dependency is using the javax.servlet: https://mvnrepository.com/artifact/org.springframework.social/spring-social-facebook/2.0.3.RELEASE

And following this github, it seems this packages is no longer actively maintained and being archived, so it is likely they do not upgrade for Spring Boot 3.

I would recommend find other ways to work on the social login for Facebook, maybe you can try this way?

https://www.codejava.net/frameworks/spring-boot/social-login-with-facebook-example

Upvotes: 54

Related Questions