我想使用 Jakarta 和 TomEE Plume 服务器创建我的第一个 REST Api。
通过 HTTP 进行部署和 api 调用是有效的,但我仍然需要修复异常,因为要学习如何正确编程。
在服务器启动时,我收到此错误消息:
12-Jan-2025 15:18:07.972 WARNUNG [http-nio-8081-exec-4] org.apache.batchee.container.services.ServicesManager.init You didn't specify org.apache.batchee.jmx.application and JMX is already registered, skipping
12-Jan-2025 15:18:09.982 SCHWERWIEGEND [http-nio-8081-exec-4] org.apache.openejb.observer.ObserverManager$MethodInvocation.invoke error invoking org.apache.tomee.microprofile.TomEEMicroProfileListener@4ef27d66
java.lang.IllegalStateException: On a thread without an initialized context nor a classloader mapping a deployed app
at org.apache.openejb.cdi.ThreadSingletonServiceImpl.get(ThreadSingletonServiceImpl.java:366)
at org.apache.openejb.cdi.ThreadSingletonServiceImpl.getContext(ThreadSingletonServiceImpl.java:342)
at org.apache.openejb.cdi.ThreadSingletonServiceImpl.get(ThreadSingletonServiceImpl.java:377)
at org.apache.openejb.cdi.ThreadSingletonServiceImpl.get(ThreadSingletonServiceImpl.java:71)
at org.apache.webbeans.config.WebBeansFinder.getSingletonInstance(WebBeansFinder.java:57)
at org.apache.webbeans.config.WebBeansContext.getInstance(WebBeansContext.java:197)
at org.apache.webbeans.config.WebBeansContext.currentInstance(WebBeansContext.java:215)
at org.apache.webbeans.container.OwbCDI.getWebBeansContext(OwbCDI.java:47)
at org.apache.webbeans.container.OwbCDI.instance(OwbCDI.java:52)
at org.apache.webbeans.container.OwbCDI.select(OwbCDI.java:97)
at io.smallrye.opentracing.SmallRyeTracingDynamicFeature.<init>(SmallRyeTracingDynamicFeature.java:30)
at org.apache.tomee.microprofile.TomEEMicroProfileListener.registerMicroProfileJaxRsProviders(TomEEMicroProfileListener.java:135)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at org.apache.openejb.observer.ObserverManager$MethodInvocation.invoke(ObserverManager.java:402)
at org.apache.openejb.observer.ObserverManager.doFire(ObserverManager.java:111)
at org.apache.openejb.observer.ObserverManager.fireEvent(ObserverManager.java:100)
at org.apache.openejb.loader.SystemInstance.fireEvent(SystemInstance.java:151)
at org.apache.openejb.server.cxf.rs.CxfRsHttpListener.configureFactory(CxfRsHttpListener.java:1328)
at org.apache.openejb.server.cxf.rs.CxfRsHttpListener.deployApplication(CxfRsHttpListener.java:665)
at org.apache.openejb.server.rest.RESTService.deployApplication(RESTService.java:546)
at org.apache.openejb.server.rest.RESTService.afterApplicationCreated(RESTService.java:263)
at org.apache.tomee.webservices.TomeeJaxRsService.afterApplicationCreated(TomeeJaxRsService.java:53)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at org.apache.openejb.observer.ObserverManager$MethodInvocation.invoke(ObserverManager.java:402)
at org.apache.openejb.observer.ObserverManager$InvocationList.invoke(ObserverManager.java:519)
at org.apache.openejb.observer.ObserverManager$BeforeAndAfterInvocationSet.invoke(ObserverManager.java:499)
at org.apache.openejb.observer.ObserverManager.doFire(ObserverManager.java:111)
at org.apache.openejb.observer.ObserverManager.fireEvent(ObserverManager.java:100)
at org.apache.openejb.loader.SystemInstance.fireEvent(SystemInstance.java:151)
at org.apache.tomee.catalina.TomcatWebAppBuilder.afterStart(TomcatWebAppBuilder.java:1819)
at org.apache.tomee.catalina.GlobalListenerSupport.lifecycleEvent(GlobalListenerSupport.java:117)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:123)
at org.apache.catalina.util.LifecycleBase.setStateInternal(LifecycleBase.java:423)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:193)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:726)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:698)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:747)
at org.apache.tomee.catalina.TomcatWebAppBuilder.deployWar(TomcatWebAppBuilder.java:687)
at org.apache.tomee.catalina.TomcatWebAppBuilder.deployWebApps(TomcatWebAppBuilder.java:616)
at org.apache.tomee.catalina.deployment.TomcatWebappDeployer.deploy(TomcatWebappDeployer.java:47)
at org.apache.openejb.assembler.DeployerEjb.deploy(DeployerEjb.java:177)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at org.apache.openejb.core.interceptor.ReflectionInvocationContext$Invocation.invoke(ReflectionInvocationContext.java:211)
at org.apache.openejb.core.interceptor.ReflectionInvocationContext.proceed(ReflectionInvocationContext.java:192)
at org.apache.openejb.security.internal.InternalSecurityInterceptor.invoke(InternalSecurityInterceptor.java:35)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at org.apache.openejb.core.interceptor.ReflectionInvocationContext$Invocation.invoke(ReflectionInvocationContext.java:211)
at org.apache.openejb.core.interceptor.ReflectionInvocationContext.proceed(ReflectionInvocationContext.java:192)
at org.apache.openejb.monitoring.StatsInterceptor.record(StatsInterceptor.java:191)
at org.apache.openejb.monitoring.StatsInterceptor.invoke(StatsInterceptor.java:101)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at org.apache.openejb.core.interceptor.ReflectionInvocationContext$Invocation.invoke(ReflectionInvocationContext.java:211)
at org.apache.openejb.core.interceptor.ReflectionInvocationContext.proceed(ReflectionInvocationContext.java:192)
at org.apache.openejb.core.interceptor.InterceptorStack.invoke(InterceptorStack.java:95)
at org.apache.openejb.core.singleton.SingletonContainer._invoke(SingletonContainer.java:272)
at org.apache.openejb.core.singleton.SingletonContainer.invoke(SingletonContainer.java:221)
at org.apache.openejb.server.ejbd.EjbRequestHandler.doEjbObject_BUSINESS_METHOD(EjbRequestHandler.java:371)
at org.apache.openejb.server.ejbd.EjbRequestHandler.processRequest(EjbRequestHandler.java:182)
at org.apache.openejb.server.ejbd.EjbDaemon.processEjbRequest(EjbDaemon.java:360)
at org.apache.openejb.server.ejbd.EjbDaemon.service(EjbDaemon.java:247)
at org.apache.openejb.server.ejbd.EjbServer.service(EjbServer.java:104)
at org.apache.openejb.server.httpd.ServerServlet.service(ServerServlet.java:60)
at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:792)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:223)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
at org.apache.tomee.catalina.OpenEJBValve.invoke(OpenEJBValve.java:45)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:119)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.tomee.catalina.OpenEJBSecurityListener$RequestCapturer.invoke(OpenEJBSecurityListener.java:97)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:356)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:870)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1762)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:1583)
12-Jan-2025 15:18:12.755 WARNUNG [Catalina-utility-2] org.apache.batchee.container.services.ServicesManager.init You didn't specify org.apache.batchee.jmx.application and JMX is already registered, skipping
但前提是包含我的第一个测试 REST Api 文件:
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
@Path("/hello")
public class HelloResources {
@GET
@Path("/test")
@Produces(MediaType.TEXT_PLAIN)
public String sayHello() {
return "Hello World";
}
}
如果需要,我可以为您提供更多详细信息。
我也面临着同样的问题。
我不知道为什么,但即使不需要 beans.xml(CDI 部署描述符),似乎将其放入 Web 应用程序存档中就可以解决问题。
它应该位于 WEB-INF 目录中:
对于 Web 应用程序,beans.xml 部署描述符(如果存在)必须位于 WEB-INF 目录中。对于 EJB 模块或 JAR 文件,beans.xml 部署描述符(如果存在)必须位于 META-INF 目录中。
按照惯例,META-INF 和 WEB-INF 文件夹位于 webapp 目录下(Web 应用程序入门 > Web 应用程序存档 > 构建示例项目)。