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.

Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
asdfkjasdfjk
  • 3,784
  • 18
  • 64
  • 104

7 Answers7

15

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

Koos Gadellaa
  • 1,220
  • 7
  • 17
  • 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](https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-sql-h2-console) for more details. – Stephane Nicoll Apr 10 '19 at 14:27
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>
Doug
  • 121
  • 1
  • 2
  • THANKS! I've just overwritten the JAR file downloaded from central with the version from the H2 website and it's now working. – jste89 Apr 13 '19 at 08:53
3

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

olasammy
  • 6,676
  • 4
  • 26
  • 32
2
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.200</version>
        <scope>compile</scope>
    </dependency>
  • 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 SO – studioj Dec 18 '20 at 16:03
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

cosmir17
  • 57
  • 7
  • 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. – Rot-man Apr 10 '19 at 22:13
  • 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. – cosmir17 Apr 10 '19 at 22:22
0

it's work for me

<dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>compile</scope>
    </dependency>
Soufiane D
  • 21
  • 3
-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

yaniv
  • 152
  • 5
  • 15