小编典典

通过IntelliJ IDEA 13部署的Tomcat War返回错误

tomcat

我在IntelliJ中部署第一个WAR文件时遇到以下错误。据说这是一个本地问题,因为这只是我们部署以测试设置的WAR文件。

所有80个Maven测试都通过了,但是当我浏览到Web应用程序时,出现此错误:

java.lang.IllegalArgumentException: javax.naming.NameNotFoundException: Name [jdbc/ipam] is not bound in this Context. Unable to find [jdbc].
    com.myapp1.ipam.rest.JaxRsApplication.<init>(JaxRsApplication.java:26)
    sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    org.glassfish.hk2.utilities.reflection.ReflectionHelper.makeMe(ReflectionHelper.java:1104)
    org.jvnet.hk2.internal.Utilities.justCreate(Utilities.java:902)
    org.jvnet.hk2.internal.ServiceLocatorImpl.create(ServiceLocatorImpl.java:872)
    org.jvnet.hk2.internal.ServiceLocatorImpl.createAndInitialize(ServiceLocatorImpl.java:964)
    org.jvnet.hk2.internal.ServiceLocatorImpl.createAndInitialize(ServiceLocatorImpl.java:956)
    org.glassfish.jersey.server.ApplicationHandler.createApplication(ApplicationHandler.java:336)
    org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:315)
    org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:285)
    org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:310)
    org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:170)
    org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:358)
    javax.servlet.GenericServlet.init(GenericServlet.java:158)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:745)

这似乎是问题所在:

    public JaxRsApplication() {
    DataSource datasource;
    try {
        Context initCtx = new InitialContext();
        Context envCtx = (Context) initCtx.lookup("java:comp/env");
        datasource = (DataSource) envCtx.lookup("jdbc/ipam");
    } catch (NamingException e) {
        throw new IllegalArgumentException(e);
    }
    this.init(new SystemConfiguration(datasource));
}

这是我第一次使用IntelliJ,也是Java的新手,所以我不确定在这里做什么。

这是 context-fragment.xml

<?xml version='1.0' encoding='utf-8'?>
<Context>
    <Resource name="jdbc/ipam"
              factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
              auth="Container"
              type="javax.sql.DataSource"
              initialSize="2"
              maxActive="5"
              maxIdle="5"
              minIdle="1"
              maxWait="10000"
              removeAbandoned="true"
              removeAbandonedTimeout="60"
              logAbandoned="true"
              defaultReadOnly="false"
              username="**********"
              password="**********"
              driverClassName="org.postgresql.Driver"
              validationQuery="select 1"
              validationQueryTimeout="5"
              logValidationErrors="true"
              testOnBorrow="true"
              testWhileIdle="true"
              url="jdbc:postgresql://db1host/ipam_dev_table_here"/>
</Context>

阅读 300

收藏
2020-06-16

共1个答案

小编典典

确保已设置IntelliJ以在启动tomcat时部署context.xml文件。你可以这样做

  1. 从文件菜单中选择“项目结构”

  2. 从那里,选择“ Facets”选项。确保已配置Web构面。如果没有,请添加一个。

  3. 添加Web构面后,选择“添加Application Server特定描述符…”

  4. 从选项中选择“ Tomcat上下文描述符”,然后单击“确定”。

默认情况下,IntelliJ将指向META-INF / context.xml。如果这不是上下文文件的位置,则需要编辑该文件的路径。

2020-06-16