tomcat 在web.xml中使用init-param导致应用程序崩溃

fiei3ece  于 2023-04-06  发布在  其他
关注(0)|答案(1)|浏览(416)

在我的应用程序(Tomcat 9,Java JAX-RS)的web.xml中配置servlet时遇到问题。经过大量的尝试和错误,我设法将问题减少到观察到web.xml中存在init-param标记会以某种方式导致服务-servlet-mapping中的url-pattern-不可用(在尝试访问它们时得到404)。

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name>Test</display-name>
  <servlet>
    <display-name>JAX-RS REST Servlet</display-name>
    <servlet-name>JAX-RS REST Servlet</servlet-name>
    <servlet-class>
      org.glassfish.jersey.servlet.ServletContainer
    </servlet-class>
    <init-param>
      <param-name>parname</param-name>
      <param-value>parval</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>JAX-RS REST Servlet</servlet-name>
    <url-pattern>/services/*</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

如果把上面的init-param从文件中取出来,我就可以访问http://localhost:8081/app/services/whatever,否则就会得到404,就好像servletMap无效一样。
我在分析 Catalina 日志时已经部署了应用程序的两个变体,除了时间戳之外没有任何区别。我认为java.lang.ClassCastException是无关紧要的...或者是吗?:)
Catalina withthe init-param:

05-Apr-2023 11:04:47.923 INFO [Catalina-utility-1] org.apache.catalina.startup.HostConfig.undeploy Undeploying context [/testDevOps]
05-Apr-2023 11:04:47.933 WARNING [Catalina-utility-1] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesObjectStreamClassCaches Failed to clear soft references from ObjectStreamClass$Caches for web application [testDevOps]
    java.lang.ClassCastException: class java.io.ObjectStreamClass$Caches$1 cannot be cast to class java.util.Map (java.io.ObjectStreamClass$Caches$1 and java.util.Map are in module java.base of loader 'bootstrap')
      at org.apache.catalina.loader.WebappClassLoaderBase.clearCache(WebappClassLoaderBase.java:2325)
      at org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesObjectStreamClassCaches(WebappClassLoaderBase.java:2300)
      at org.apache.catalina.loader.WebappClassLoaderBase.clearReferences(WebappClassLoaderBase.java:1669)
      at org.apache.catalina.loader.WebappClassLoaderBase.stop(WebappClassLoaderBase.java:1597)
      at org.apache.catalina.loader.WebappLoader.stopInternal(WebappLoader.java:463)
      at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:257)
      at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5515)
      at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:257)
      at org.apache.catalina.core.ContainerBase.removeChild(ContainerBase.java:814)
      at org.apache.catalina.startup.HostConfig.undeploy(HostConfig.java:1466)
      at org.apache.catalina.startup.HostConfig.checkResources(HostConfig.java:1391)
      at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1654)
      at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:315)
      at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:123)
      at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1162)
      at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1365)
      at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1369)
      at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1347)
      at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
      at java.base/java.util.concurrent.FutureTask.runAndReset(FutureTask.java:305)
      at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:305)
      at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
      at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
      at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
      at java.base/java.lang.Thread.run(Thread.java:829)
05-Apr-2023 11:04:51.828 INFO [http-nio-8081-exec-7] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive [/var/lib/tomcat9/webapps/testDevOps.war]
05-Apr-2023 11:04:52.340 INFO [http-nio-8081-exec-7] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
05-Apr-2023 11:04:52.439 WARNING [http-nio-8081-exec-7] org.glassfish.jersey.message.internal.MessagingBinders$EnabledProvidersBinder.bindToBinder A class javax.activation.DataSource for a default provider MessageBodyWriter<javax.activation.DataSource> was not found. The provider is not available.
05-Apr-2023 11:04:52.448 WARNING [http-nio-8081-exec-7] org.glassfish.jersey.server.wadl.WadlFeature.configure JAX-B API not found . WADL feature is disabled.
05-Apr-2023 11:04:52.500 INFO [http-nio-8081-exec-7] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive [/var/lib/tomcat9/webapps/testDevOps.war] has finished in [673] ms

Catalina withoutthe init-param

05-Apr-2023 11:06:38.449 INFO [Catalina-utility-1] org.apache.catalina.startup.HostConfig.undeploy Undeploying context [/testDevOps]
05-Apr-2023 11:06:38.460 WARNING [Catalina-utility-1] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesObjectStreamClassCaches Failed to clear soft references from ObjectStreamClass$Caches for web application [testDevOps]
    java.lang.ClassCastException: class java.io.ObjectStreamClass$Caches$1 cannot be cast to class java.util.Map (java.io.ObjectStreamClass$Caches$1 and java.util.Map are in module java.base of loader 'bootstrap')
      at org.apache.catalina.loader.WebappClassLoaderBase.clearCache(WebappClassLoaderBase.java:2325)
      at org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesObjectStreamClassCaches(WebappClassLoaderBase.java:2300)
      at org.apache.catalina.loader.WebappClassLoaderBase.clearReferences(WebappClassLoaderBase.java:1669)
      at org.apache.catalina.loader.WebappClassLoaderBase.stop(WebappClassLoaderBase.java:1597)
      at org.apache.catalina.loader.WebappLoader.stopInternal(WebappLoader.java:463)
      at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:257)
      at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5515)
      at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:257)
      at org.apache.catalina.core.ContainerBase.removeChild(ContainerBase.java:814)
      at org.apache.catalina.startup.HostConfig.undeploy(HostConfig.java:1466)
      at org.apache.catalina.startup.HostConfig.checkResources(HostConfig.java:1391)
      at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1654)
      at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:315)
      at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:123)
      at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1162)
      at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1365)
      at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1369)
      at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1347)
      at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
      at java.base/java.util.concurrent.FutureTask.runAndReset(FutureTask.java:305)
      at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:305)
      at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
      at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
      at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
      at java.base/java.lang.Thread.run(Thread.java:829)
05-Apr-2023 11:06:39.945 INFO [http-nio-8081-exec-12] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive [/var/lib/tomcat9/webapps/testDevOps.war]
05-Apr-2023 11:06:40.400 INFO [http-nio-8081-exec-12] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
05-Apr-2023 11:06:40.506 WARNING [http-nio-8081-exec-12] org.glassfish.jersey.message.internal.MessagingBinders$EnabledProvidersBinder.bindToBinder A class javax.activation.DataSource for a default provider MessageBodyWriter<javax.activation.DataSource> was not found. The provider is not available.
05-Apr-2023 11:06:40.512 WARNING [http-nio-8081-exec-12] org.glassfish.jersey.server.wadl.WadlFeature.configure JAX-B API not found . WADL feature is disabled.
05-Apr-2023 11:06:40.594 INFO [http-nio-8081-exec-12] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive [/var/lib/tomcat9/webapps/testDevOps.war] has finished in [650] ms

对进一步的步骤有何建议?

3phpmpom

3phpmpom1#

您的应用程序WAR文件名为testDevOps.war。这通常会导致将Web应用程序部署到以下上下文路径:

http://localhost:8081/testDevOps/

尝试使用如下URL访问您的服务:

http://localhost:8081/testDevOps/services/whatever

有关定义上下文,请参见Tomcat配置参考。

相关问题