Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法导入org.h2.server.web.WebServlet_Java_Maven_Spring Boot_H2 - Fatal编程技术网

Java 无法导入org.h2.server.web.WebServlet

Java 无法导入org.h2.server.web.WebServlet,java,maven,spring-boot,h2,Java,Maven,Spring Boot,H2,我正在尝试将我的spring引导应用程序配置为使用h2控制台。我找到了一些文章,它们都使用webServlet。但是,尽管在pom.xml中添加了h2依赖项,但我无法导入该类。我收到此错误消息无法解析符号WebServlet。我的进口线 import org.h2.server.web.WebServlet; 下面是我的pom.xml <dependency> <groupId>org.springframework.boot</group

我正在尝试将我的spring引导应用程序配置为使用h2控制台。我找到了一些文章,它们都使用webServlet。但是,尽管在pom.xml中添加了h2依赖项,但我无法导入该类。我收到此错误消息
无法解析符号WebServlet
。我的进口线

import org.h2.server.web.WebServlet;
下面是我的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>
和配置

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

让我知道我错过了什么

h2不应该是
编译
(而不是
运行时
)依赖项吗?

我遇到了同样的问题,可能运行了相同的示例。具有正确的maven pom.xml依赖项,但出于某种原因,必须直接从maven下载h2驱动程序jar。然后上面的代码工作了。去掉“runtime”元素将默认为编译

    <!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.192</version>
    </dependency>

com.h2数据库
氢
1.4.192

只需从h2依赖项中删除运行时,一切都会很好

仅供参考,“运行时”范围是设计的默认范围。原因在下面的Github页面中提到。(我让你来决定)


com.h2数据库
氢
1.4.200
编译

如果从H2驱动程序导入一个类,则应该编译该类。但是Spring Boot还可以自动公开H2 web控制台,这样您就不必做所有这些事情,查看更多详细信息。虽然此链接可以回答问题,但最好在此处包含答案的基本部分,并提供链接供参考。如果链接页面更改,仅链接的答案可能会无效。无需担心。它是企业组织的Github链接。我希望会有比我更清楚的人。谢谢!我刚刚用H2网站上的版本覆盖了从central下载的JAR文件,现在可以工作了。这解决了错误。很好,你找到了解决方案。如果你能多解释一点答案,问问题的人会更清楚。通过这种方式,解决方案也会更容易被其他人理解。所以thx和welkcome to So
    <!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.192</version>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.200</version>
        <scope>compile</scope>
    </dependency>