spring HTTP 500 - 无法为连接 URL 'null' 创建类 '' 的 JDBC 驱动程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21031603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 06:27:14  来源:igfitidea点击:

HTTP 500 - Cannot create JDBC driver of class '' for connect URL 'null'

eclipsespringmaventomcat7jndi

提问by user3178994

I have been searching to fix that problem for 3 days reading forums... Then I decided to ask for some help. Nedless to say that your help would be highly appreciated :-)

我一直在寻找解决这个问题的 3 天阅读论坛......然后我决定寻求一些帮助。毋庸置疑,您的帮助将不胜感激:-)

I created a jndi resource for the mysql connexion in Tomcat context.xml instance in Eclipse My web app works fine when I start the server from eclipse. It doesn't work fine when I start the web app from eclipse 'Run configurations' with that goals : clean tomcat7:run. My aim is to delegate the build, deploy, start server to maven from eclipse.

我在 Eclipse 中的 Tomcat context.xml 实例中为 mysql 连接创建了一个 jndi 资源当我从 Eclipse 启动服务器时,我的 web 应用程序工作正常。当我从具有该目标的 eclipse“运行配置”启动 Web 应用程序时,它无法正常工作:clean tomcat7:run。我的目标是将构建、部署、启动服务器从 Eclipse 委托给 Maven。

I've put the mysql-connector-java-5.1.27.jar in Tomcat_Home/lib.

我已将 mysql-connector-java-5.1.27.jar 放在 Tomcat_Home/lib 中。

I don't want to put the datasource configuration in the app (no META-INF....) as one goal of jndi is to place the datasource info out of the app.

我不想将数据源配置放在应用程序中(没有 META-INF ....),因为 jndi 的一个目标是将数据源信息放在应用程序之外。

Thank you for reading and for the time you'll spend to find a solution.

感谢您阅读并花时间寻找解决方案。

Technical specifications

技术规格

  • eclipse : juno release2
  • spring tool suite : 3.4
  • wtp
  • m2e : 1.4
  • maven : 3.0.2
  • jdk : 1.6.21
  • tomcat : 7.0.25

    mysql : 5.6.14

  • 日食:juno release2
  • 弹簧工具套件:3.4
  • 重量级
  • m2e : 1.4
  • 行家:3.0.2
  • JDK:1.6.21
  • 雄猫:7.0.25

    mysql:5.6.14

stacktrace

堆栈跟踪

type Exception report

message Request processing failed; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'

description The server encountered an internal error that prevented it from fulfilling this request.

exception 

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:927)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)


root cause 

org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
    org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80)
    org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:573)
    org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:637)
    org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:662)
    org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:702)
    org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.queryForObject(NamedParameterJdbcTemplate.java:178)
    com.marc.springmvc3.dao.PersonDAO.getPersonById(PersonDAO.java:26)
    com.marc.springmvc3.service.PersonService.getAllPersons(PersonService.java:18)
    com.marc.springmvc3.mvc.PersonController.getListOfPerson(PersonController.java:33)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:597)
    org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    org.springframework.web.method.annotation.ModelFactory.invokeModelAttributeMethods(ModelFactory.java:123)
    org.springframework.web.method.annotation.ModelFactory.initModel(ModelFactory.java:97)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:723)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:687)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)


root cause 

org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
    org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1452)
    org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
    org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
    org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)
    org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)
    org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:573)
    org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:637)
    org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:662)
    org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:702)
    org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.queryForObject(NamedParameterJdbcTemplate.java:178)
    com.marc.springmvc3.dao.PersonDAO.getPersonById(PersonDAO.java:26)
    com.marc.springmvc3.service.PersonService.getAllPersons(PersonService.java:18)
    com.marc.springmvc3.mvc.PersonController.getListOfPerson(PersonController.java:33)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:597)
    org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    org.springframework.web.method.annotation.ModelFactory.invokeModelAttributeMethods(ModelFactory.java:123)
    org.springframework.web.method.annotation.ModelFactory.initModel(ModelFactory.java:97)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:723)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:687)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)


root cause 

java.lang.NullPointerException
    sun.jdbc.odbc.JdbcOdbcDriver.getProtocol(JdbcOdbcDriver.java:507)
    sun.jdbc.odbc.JdbcOdbcDriver.knownURL(JdbcOdbcDriver.java:476)
    sun.jdbc.odbc.JdbcOdbcDriver.acceptsURL(JdbcOdbcDriver.java:307)
    java.sql.DriverManager.getDriver(DriverManager.java:253)
    org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1437)
    org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
    org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
    org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)
    org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)
    org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:573)
    org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:637)
    org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:662)
    org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:702)
    org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.queryForObject(NamedParameterJdbcTemplate.java:178)
    com.marc.springmvc3.dao.PersonDAO.getPersonById(PersonDAO.java:26)
    com.marc.springmvc3.service.PersonService.getAllPersons(PersonService.java:18)
    com.marc.springmvc3.mvc.PersonController.getListOfPerson(PersonController.java:33)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:597)
    org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    org.springframework.web.method.annotation.ModelFactory.invokeModelAttributeMethods(ModelFactory.java:123)
    org.springframework.web.method.annotation.ModelFactory.initModel(ModelFactory.java:97)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:723)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:687)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)


note The full stack trace of the root cause is available in the Apache Tomcat/7.0.47 logs.

pom.xml

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
                    http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>com.marc</groupId>
<artifactId>spring-mvc-3</artifactId>
<name>spring-mvc-3</name>
<packaging>war</packaging>
<version>1.0.0-BUILD-SNAPSHOT</version>

<properties>
    <java-version>1.6</java-version>
    <org.springframework-version>3.2.0.RELEASE</org.springframework-version>
    <org.aspectj-version>1.6.10</org.aspectj-version>
    <org.slf4j-version>1.6.1</org.slf4j-version>
</properties>

<dependencies>
    <!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${org.springframework-version}</version>
        <exclusions>
            <!-- Exclude Commons Logging in favor of SLF4j -->
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
             </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
    <!-- AspectJ -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>${org.aspectj-version}</version>
    </dependency>   

    <!-- Logging -->

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${org.slf4j-version}</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>${org.slf4j-version}</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>${org.slf4j-version}</version>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.16</version>
        <scope>runtime</scope>
    </dependency>

    <!-- @Inject -->
    <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
    </dependency>

    <!-- JSR 303 with Hibernate Validator -->
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.0.0.GA</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>4.1.0.Final</version>
    </dependency>

    <!--  MYSQL driver-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.27</version>
    </dependency>

    <!-- JDBC -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>

    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>20030825.184428</version>
    </dependency>

    <!-- Test -->
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>1.9.5</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.10</version>
        <scope>test</scope>
    </dependency>


    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
</dependencies>

<build>
    <finalName>spring-mvc-3</finalName>
    <plugins>
        <!-- Maven Tomcat Plugin -->
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <url>http://127.0.0.1:8080/manager/text</url>
                <server>TomcatServer</server>
                <path>/${project.build.finalName}</path>
                <port>8080</port>
                <charset>UTF-8</charset>
                <finalName>${project.build.finalName}</finalName>
                <username>ssss</username>
                <password>dddddddddd</password>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>5.1.27</version>
                </dependency>
            </dependencies>
        </plugin>

        <!-- Maven compiler plugin -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>                
            <configuration>
                <source>${java-version}</source>
                <target>${java-version}</target>
            </configuration>
        </plugin>
    </plugins>
</build>

Servers/tomcat-config/tomcat-users.xml (in Eclipse)

服务器/tomcat-config/tomcat-users.xml(在Eclipse中)

    <role rolename="admin"/>
  <user username="ssss" password="dddddddddd" roles="admin"/>

web.xml

网页.xml

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
                        http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Spring Web MVC Application</display-name>

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- database jndi reference -->
    <description>MySQL Test App</description>
    <resource-ref>
        <description>DB Connection</description>
        <res-ref-name>jdbc/TestDB</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>
</web-app>

mvc-dispatcher-servlet.xml

mvc-dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/jee 
        http://www.springframework.org/schema/jee/spring-jee-3.2.xsd">

    <jee:jndi-lookup id="dataSource"
        jndi-name="jdbc/TestDB"
        expected-type="javax.sql.DataSource"
        resource-ref="true"/>   

    <bean id="personDao" class="com.marc.springmvc3.dao.PersonDAO">
        <property name="dataSource" ref="dataSource"/>
        </bean> 

tomcat context.xml (in eclipse)

tomcat context.xml(在 eclipse 中)

<?xml version="1.0" encoding="UTF-8"?>
<Context>

    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>

    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->

    <!-- Uncomment this to enable Comet connection tacking (provides events
         on session expiration as well as webapp lifecycle) -->
    <!--
    <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
    -->

 <Resource  name="jdbc/TestDB" 
            username="xxx" 
            password="xxxxxxxx" 
            driverClassName="com.mysql.jdbc.Driver"
            url="jdbc:mysql://localhost:3306/mytest"
            auth="Container" 
            type="javax.sql.DataSource"
            maxActive="100" 
            maxIdle="30" 
            maxWait="10000"
            initialSize="1"/>


<!--      <ResourceLink name="jdbc/TestDB"
        global="jdbc/TestDB"
        type="javax.sql.DataSource"/> -->
</Context>

回答by MariuszS

I think your tomcat installation is not using your context.xml. Please create fresh tomcat, configure context.xmland libs and run Tomcat directly from system (without Eclipse etc).

我认为您的 tomcat 安装没有使用您的context.xml. 请创建新的 tomcat,配置context.xml和库并直接从系统运行 Tomcat(不包括 Eclipse 等)。



Question was updated, answer below is obsolete :/

问题已更新,以下答案已过时:/

The important part is:

重要的部分是:

Cannot create JDBC driver of class '' for connect URL 'null'

Your driver class is nulland connection URL is null.

您的驱动程序类是null,连接 URL 是null.

Application configuration requires configured resource jdbc/TestDBon server:

应用程序配置需要jdbc/TestDB在服务器上配置资源:

<resource-ref>
    <description>DB Connection</description>
    <res-ref-name>jdbc/TestDB</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

and

  <jee:jndi-lookup id="dataSource"
    jndi-name="jdbc/TestDB"
    expected-type="javax.sql.DataSource"
    resource-ref="true"/>   

But your Tomcat configuration Servers/tomcat-config/context.xmlis wrong. It looks like spring application configuration.

但是你的Tomcat配置Servers/tomcat-config/context.xml是错误的。它看起来像 spring 应用程序配置。

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
                        http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

 ...

Standard Tomcat context.xmllooks like this:

标准的 Tomcatcontext.xml看起来像这样:

<?xml version='1.0' encoding='utf-8'?>
<Context>
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource" 
    username="user" password="pass" 
    driverClassName="com.mysql.jdbc.Driver" 
    url="jdbc:mysql://localhost:3306/testDB?characterEncoding=utf8" maxActive="8"/>

 </Context>

回答by user3178994

I've eventually found out the solution and I'll share it with those who are encountering the same problem.

我最终找到了解决方案,并将与遇到相同问题的人分享。

Create the META-INF folder under src/main/webapp as WEB-INF sibling Move the Tomcat-config/context.xml file to the META-INF folder. You'll see in the pom.xml that this file is not going to be part of the WAR file.

在 src/main/webapp 下创建 META-INF 文件夹作为 WEB-INF 兄弟 将 Tomcat-config/context.xml 文件移动到 META-INF 文件夹。您将在 pom.xml 中看到该文件不会成为 WAR 文件的一部分。

META-INF/context.xml

META-INF/context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context>

    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>

 <Resource  name="jdbc/TestDB" 
            username="xxxx" 
            password="xxxxxxxxxx" 
            driverClassName="com.mysql.jdbc.Driver"
            url="jdbc:mysql://localhost:3306/TestDB"
            auth="Container" 
            type="javax.sql.DataSource"
            maxActive="100" 
            maxIdle="30" 
            maxWait="10000"
            initialSize="1"/>
</Context>

Adapt the pom.xml

适配 pom.xml

<plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <url>http://127.0.0.1:8080/manager/text</url>
                <server>TomcatServer</server> <!-- user + password defined in the Maven/conf/settings.xml-->
                <path>/${project.build.finalName}</path>
                <port>8080</port>
                <charset>UTF-8</charset>
                <finalName>${project.build.finalName}</finalName>
                <contextFile>${project.basedir}/src/main/webapp/META-INF/context.xml</contextFile> <!--The path of the Tomcat context XML file. This is not used for war deployment mode.-->
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId> <!-- driver mysql -->
                    <version>5.1.27</version>
                </dependency>
            </dependencies>
        </plugin>

MAVEN_HOME/conf/settings.xml The id is referenced in the pom.xml. The username and the password are declared in the tomcat-users.xml

MAVEN_HOME/conf/settings.xml pom.xml 中引用了 id。用户名和密码在 tomcat-users.xml 中声明

<server>
    <id>TomcatServer</id>
    <username>xxxxx</username>
    <password>xxxxxxxxx</password>
</server>

Eclipse / Run - Run configurations ...

Eclipse / 运行 - 运行配置...

  • create new launch configuration
  • Base direrctory ${workspace_loc:/your project name}
  • Goals clean tomcat7:run

  • Apply

  • Run
  • 创建新的启动配置
  • 基本目录 ${workspace_loc:/您的项目名称}
  • 目标清理 tomcat7:run

  • 申请

Next time you can click on the arrow near the Run As... button in the ToolBar and select the configuration you want to run. The Stop and Relaunch buttons are very useful.

下次您可以单击工具栏中 Run As... 按钮附近的箭头,然后选择要运行的配置。停止和重新启动按钮非常有用。

回答by Guilherme

I am using Eclipse STS 3.9.3, tomcat 8.5 and Spring Boot 2.0.0.RELEASE

我正在使用 Eclipse STS 3.9.3、tomcat 8.5 和 Spring Boot 2.0.0.RELEASE

I spent my entire morning about this problem. So, I did a lot of things to solve:

我整个上午都在讨论这个问题。所以,我做了很多事情来解决:

  1. Remove jdbc dependence from my pom.

        <!-- dependency>
           <groupId>org.postgresql</groupId>
           <artifactId>postgresql</artifactId>
           <scope>runtime</scope>
        </dependency-->
    
  2. Declaration resource TOMCAT_HOME/CONF/server.xml whit factory:

    <GlobalNamingResources>
     ...
       <Resource name="jdbc/postgres_jndi"
                  auth="Container"
                  type="javax.sql.DataSource"
                  driverClassName="org.postgresql.Driver"
                  url="jdbc:postgresql://xxx.xxx.xx.xxx:5432/db"
                  factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
                  removeAbandonedOnBorrow="true"
                  removeAbandonedOnMaintenance="true"
                  timeBetweenEvictionRunsMillis="10000"
                  removeAbandonedTimeout="60"
                  logAbandoned="true"
                  username="xxx"
                  password="xxxxxx"
                  maxTotal="20"
                  maxIdle="10"
                  maxWaitMillis="-1"/>
     ...
    </GlobalNamingResources>
    

    I realized that I was using Eclipse STS. The tomcat resource on server.xml only works after I put the resource declaration in server.xml inside of "Tomcat v8.5 Server at localhost-config" folder in my Server project. Did not work on TOMCAT_HOME/conf folder in server.xml file.

  3. Change my application.yml to:

    spring:
      datasource:
        platform: postgres
        jndi-name: java:comp/env/jdbc/postgres_jndi
        type: javax.sql.DataSource
        driver-class-name: org.postgresql.Driver
      jpa:
        hibernate:
          ddl-auto: validate
        database-platform: org.hibernate.dialect.PostgreSQL9Dialect
        database: POSTGRESQL
        show-sql: true
        #Method org.postgresql.jdbc.PgConnection.createClob() is not yet implemented.
        properties:
          hibernate:
            temp:
              use_jdbc_metadata_defaults: false
    
  1. 从我的 pom 中删除 jdbc 依赖。

        <!-- dependency>
           <groupId>org.postgresql</groupId>
           <artifactId>postgresql</artifactId>
           <scope>runtime</scope>
        </dependency-->
    
  2. 声明资源TOMCAT_HOME/CONF/server.xml whit factory:

    <GlobalNamingResources>
     ...
       <Resource name="jdbc/postgres_jndi"
                  auth="Container"
                  type="javax.sql.DataSource"
                  driverClassName="org.postgresql.Driver"
                  url="jdbc:postgresql://xxx.xxx.xx.xxx:5432/db"
                  factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
                  removeAbandonedOnBorrow="true"
                  removeAbandonedOnMaintenance="true"
                  timeBetweenEvictionRunsMillis="10000"
                  removeAbandonedTimeout="60"
                  logAbandoned="true"
                  username="xxx"
                  password="xxxxxx"
                  maxTotal="20"
                  maxIdle="10"
                  maxWaitMillis="-1"/>
     ...
    </GlobalNamingResources>
    

    我意识到我正在使用 Eclipse STS。server.xml 上的 tomcat 资源仅在我将 server.xml 中的资源声明放在我的服务器项目中的“Tomcat v8.5 Server at localhost-config”文件夹中后才有效。对 server.xml 文件中的 TOMCAT_HOME/conf 文件夹不起作用。

  3. 将我的 application.yml 更改为:

    spring:
      datasource:
        platform: postgres
        jndi-name: java:comp/env/jdbc/postgres_jndi
        type: javax.sql.DataSource
        driver-class-name: org.postgresql.Driver
      jpa:
        hibernate:
          ddl-auto: validate
        database-platform: org.hibernate.dialect.PostgreSQL9Dialect
        database: POSTGRESQL
        show-sql: true
        #Method org.postgresql.jdbc.PgConnection.createClob() is not yet implemented.
        properties:
          hibernate:
            temp:
              use_jdbc_metadata_defaults: false
    

A application.properties will be like:

application.properties 将类似于:

    spring.datasource.platform= postgres
    spring.datasource.jndi-name= java:comp/env/jdbc/postgres_jndi
    spring.datasource.type= javax.sql.DataSource
    spring.datasource.driver-class-name= org.postgresql.Driver
    spring.jpa.hibernate.ddl-auto= validate
    spring.jpa.hibernatedatabase-platform= org.hibernate.dialect.PostgreSQL9Dialect
    spring.jpa.database= POSTGRESQL
    spring.jpa.show-sql= true
    spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults=false
  1. Define a context.xml file in src/main/webapp/META-INF/context.xml with definition:

    <?xml version="1.0" encoding="UTF-8"?>
    <Context path="/myapp">
        <ResourceLink global="jdbc/postgres_jndi" name="jdbc/postgres_jni" type="javax.sql.DataSource"/>
    </Context>
    
  2. Put the file postgresql-42.2.2.jar in the TOMCAT_HOME/lib

  1. 在 src/main/webapp/META-INF/context.xml 中定义一个 context.xml 文件并定义:

    <?xml version="1.0" encoding="UTF-8"?>
    <Context path="/myapp">
        <ResourceLink global="jdbc/postgres_jndi" name="jdbc/postgres_jni" type="javax.sql.DataSource"/>
    </Context>
    
  2. 将文件 postgresql-42.2.2.jar 放入 TOMCAT_HOME/lib