【问题标题】:How can I access my REST resource with wildfly?如何使用 wildfly 访问我的 REST 资源?
【发布时间】:2017-12-19 15:07:02
【问题描述】:

您好,我正在使用 IntelliJ 开发一个 Rest API,遵循 the official doc

但我在访问资源时收到 404 Not found 错误...我仍然可以访问 index.jsp 页面。我遵循 de doc 的每一步,但我使用的是 Wildfly 而不是 Glassfish。

这里是资源:

@Path("devices")
public class DevicesResource {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getAllDevices() {
        return "Hello world from thingsplay.";
    }

}

这是 Application 子类:

@ApplicationPath("/api/v2.0")
public class ApplicationConfig extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> resources = new HashSet<>();
        addResourceClasses(resources);
        return resources;
    }

    public void addResourceClasses(Set<Class<?>> resources) {
        resources.add(be.thingsplay.resources.DevicesResource.class);
    }

}

因此,当我访问 http://localhost:8080/api/v2.0 时,我得到了索引页面,但是当我尝试访问 http://localhost:8080/api/v2.0/devices 时,我得到了 404 Not Found 错误。

当我查看工件时,我注意到球衣库并未包含在战争中。但是当我尝试将其添加到战争中时,当我尝试部署应用程序时会遇到异常。

我得到的例外:

14:57:23,799 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-3) WFLYSRV0027: Starting deployment of "apirest_war_exploded.war" (runtime-name: "apirest_war_exploded.war")
14:57:24,147 WARN  [org.jboss.weld.deployer] (MSC service thread 1-2) WFLYWELD0013: Deployment deployment "apirest_war_exploded.war" contains CDI annotations but no bean archive was not found. (No beans.xml nor class with bean defining annotations)
14:57:24,178 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 89) MSC000001: Failed to start service jboss.undertow.deployment.default-server.default-host./apirest_war_exploded: org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./apirest_war_exploded: java.lang.NoClassDefFoundError: com/google/common/base/Function
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:85)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
    at org.jboss.threads.JBossThread.run(JBossThread.java:320)
Caused by: java.lang.NoClassDefFoundError: com/google/common/base/Function
    at org.glassfish.jersey.internal.ServiceFinder.<clinit>(ServiceFinder.java:165)
    at org.glassfish.jersey.servlet.internal.ServletContainerProviderFactory.getAllServletContainerProviders(ServletContainerProviderFactory.java:66)
    at org.glassfish.jersey.servlet.init.JerseyServletContainerInitializer.onStartup(JerseyServletContainerInitializer.java:132)
    at io.undertow.servlet.core.DeploymentManagerImpl$1.call(DeploymentManagerImpl.java:186)
    at io.undertow.servlet.core.DeploymentManagerImpl$1.call(DeploymentManagerImpl.java:171)
    at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:42)
    at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
    at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
    at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
    at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
    at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
    at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
    at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:234)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:100)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:82)
    ... 6 more
Caused by: java.lang.ClassNotFoundException: com.google.common.base.Function from [Module "deployment.apirest_war_exploded.war:main" from Service Module Loader]
    at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:198)
    at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:363)
    at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:351)
    at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:93)
    ... 21 more

14:57:24,182 ERROR [org.jboss.as.controller.management-operation] (management-handler-thread - 20) WFLYCTL0013: Operation ("deploy") failed - address: ([("deployment" => "apirest_war_exploded.war")]) - failure description: {
    "WFLYCTL0080: Failed services" => {"jboss.undertow.deployment.default-server.default-host./apirest_war_exploded" => "org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./apirest_war_exploded: java.lang.NoClassDefFoundError: com/google/common/base/Function
    Caused by: java.lang.NoClassDefFoundError: com/google/common/base/Function
    Caused by: java.lang.ClassNotFoundException: com.google.common.base.Function from [Module \"deployment.apirest_war_exploded.war:main\" from Service Module Loader]"},
    "WFLYCTL0412: Required services that are not installed:" => ["jboss.undertow.deployment.default-server.default-host./apirest_war_exploded"],
    "WFLYCTL0180: Services with missing/unavailable dependencies" => undefined
}
14:57:24,183 ERROR [org.jboss.as.server] (management-handler-thread - 20) WFLYSRV0021: Deploy of deployment "apirest_war_exploded.war" was rolled back with the following failure message: 
{
    "WFLYCTL0080: Failed services" => {"jboss.undertow.deployment.default-server.default-host./apirest_war_exploded" => "org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./apirest_war_exploded: java.lang.NoClassDefFoundError: com/google/common/base/Function
    Caused by: java.lang.NoClassDefFoundError: com/google/common/base/Function
    Caused by: java.lang.ClassNotFoundException: com.google.common.base.Function from [Module \"deployment.apirest_war_exploded.war:main\" from Service Module Loader]"},
    "WFLYCTL0412: Required services that are not installed:" => ["jboss.undertow.deployment.default-server.default-host./apirest_war_exploded"],
    "WFLYCTL0180: Services with missing/unavailable dependencies" => undefined
}
14:57:24,207 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-3) WFLYSRV0028: Stopped deployment apirest_war_exploded.war (runtime-name: apirest_war_exploded.war) in 23ms
14:57:24,208 INFO  [org.jboss.as.controller] (management-handler-thread - 20) WFLYCTL0183: Service status report
WFLYCTL0184:    New missing/unsatisfied dependencies:
      service jboss.undertow.deployment.default-server.default-host./apirest_war_exploded (missing) dependents: [service jboss.deployment.unit."apirest_war_exploded.war".deploymentCompleteService] 
WFLYCTL0186:   Services which failed to start:      service jboss.undertow.deployment.default-server.default-host./apirest_war_exploded

[2017-07-14 02:57:24,302] Artifact apirest:war exploded: Error during artifact deployment. See server log for details.
[2017-07-14 02:57:24,302] Artifact apirest:war exploded: java.lang.Exception: {"WFLYCTL0080: Failed services" => {"jboss.undertow.deployment.default-server.default-host./apirest_war_exploded" => "org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./apirest_war_exploded: java.lang.NoClassDefFoundError: com/google/common/base/Function
    Caused by: java.lang.NoClassDefFoundError: com/google/common/base/Function
    Caused by: java.lang.ClassNotFoundException: com.google.common.base.Function from [Module \"deployment.apirest_war_exploded.war:main\" from Service Module Loader]"},"WFLYCTL0412: Required services that are not installed:" => ["jboss.undertow.deployment.default-server.default-host./apirest_war_exploded"],"WFLYCTL0180: Services with missing/unavailable dependencies" => undefined}

【问题讨论】:

  • 尝试在@Path("/devices")路径中使用/
  • 当前发布的异常堆栈跟踪是一个红鲱鱼,因为它是由错误地捆绑您不需要捆绑的库引起的。请扭转这一点,而是在您无法访问资源的地方发布部署日志。
  • @tima 我已经尝试过了,它不会改变任何事情。
  • @Gimby 当我收到 404 Not Found 错误时没有记录错误。
  • 我没有要求错误,我要求记录。

标签: java rest jakarta-ee wildfly


【解决方案1】:

首先,您不需要向应用程序添加任何 REST 实现 jar(例如 Jersey),因为 WildFly 和 GlassFish 等 Java EE 7 服务器是免费提供的。

问题的症结在于您的 URL 缺少应用程序的上下文路径(在您引用的教程中为 rest_glassfish_hello_world_war_exploded)。

即。正确的服务 URL 应该是这样的:

http://localhost:8080/rest_glassfish_hello_world_war_exploded/api/v2.0/devices

如果 rest_glassfish_hello_world_war_exploded 不同,则应将其替换为您的实际应用程序名称。

【讨论】:

  • 好吧,我可以通过localhost:8080/api/v2.0 访问index.jsp。
  • 您是否明确将您的应用配置为可从根“/”上下文访问?
猜你喜欢
  • 2012-11-30
  • 1970-01-01
  • 2018-10-11
  • 1970-01-01
  • 1970-01-01
  • 2013-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多