Cannot import org.h2.server.web.WebServlet
Asked Answered
P

7

12

I am trying to configure my spring boot application to use h2 console. I found some articles and all of them use webServlet. But I can not import the class although I have h2 dependency added in my pom.xml. I get this error message can not resolve the symbol WebServlet. My import line

import org.h2.server.web.WebServlet;

Below is my pom.xml

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.myfaces.core</groupId>
            <artifactId>myfaces-impl</artifactId>
            <version>2.2.6</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.myfaces.core</groupId>
            <artifactId>myfaces-api</artifactId>
            <version>2.2.6</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-core</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-logging-juli</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.1.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>5.1</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.primefaces.extensions</groupId>
            <artifactId>all-themes</artifactId>
            <version>1.0.8</version>
        </dependency>
        <dependency>
            <groupId>org.ocpsoft.rewrite</groupId>
            <artifactId>rewrite-servlet</artifactId>
            <version>2.0.12.Final</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>

and my application.properties

spring.datasource.url=jdbc:h2:mem:AZ;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

and configuration

@Bean
public ServletRegistrationBean h2servletRegistration() {
    ServletRegistrationBean registration = new ServletRegistrationBean(new WebServlet());
    registration.addUrlMappings("/console/*");
    return registration;
}

Let me know what I am missing here.

Parrie answered 1/7, 2016 at 18:0 Comment(0)
M
14

Shouldn't h2 be a compile (instead of runtime) dependency?

Marguerite answered 1/7, 2016 at 18:6 Comment(1)
If you import a class from the H2 driver, yes it should be compile. But Spring Boot can also expose automatically the H2 web console so that you don't have to do all that stuff, review the reference guide for more details.Chromatism
R
12

I had the same problem, probably running the same example. Had the correct maven pom.xml dependency but for some reason had to download the h2 driver jar directly from Maven. Then the code above worked. Taking away "runtime" element will default to compile.

    <!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.192</version>
    </dependency>
Revere answered 3/9, 2016 at 18:8 Comment(1)
THANKS! I've just overwritten the JAR file downloaded from central with the version from the H2 website and it's now working.Straightout
S
3

Just remove runtime form your h2 dependency and everything will be fine

Serve answered 19/9, 2017 at 9:10 Comment(0)
S
2
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.200</version>
        <scope>compile</scope>
    </dependency>
Sumikosumma answered 18/12, 2020 at 15:49 Comment(1)
great you found a solution. It would be alot clearer to the person asking the question if you could explain the answer a bit more. In this way the solution would also be more understandable for other people ending up over here. So thx and welkcome to SOLatif
C
0

FYI, 'runtime' scope is default by design. The reason is mentioned in the following Github page. (I leave it to you to decide) https://github.com/spring-projects/spring-boot/issues/16509

Calamanco answered 10/4, 2019 at 21:32 Comment(2)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.Chercherbourg
No worries. It's a Github link of an enterprise organisation. I was hoping that there will be people who can clarify better than me.Calamanco
G
0

it's work for me

<dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>compile</scope>
    </dependency>
Groundspeed answered 17/6, 2022 at 10:15 Comment(0)
B
-1

My issue was cannot access javax.servlet.http.HttpServlet

i managed to solve it by changing the parent version in pom.xml from 3.0.0 to 2.4.2

however annotations @Entity and @Table stopped working

so i had to switch from import jakarta.persistence.*;

to : import javax.persistence.*;

and now it is working again

Beware answered 11/1, 2023 at 8:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.