如何使用wildfly访问我的REST资源?

时间:2017-07-14 13:27:53

标签: java rest java-ee wildfly

您好我正在the official doc之后使用IntelliJ开发Rest API。

但是我在访问资源时遇到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错误。

当我看到这件神器时,我注意到战争中的球衣库并没有包括在内。但是当我尝试将它添加到战争中时,当我尝试部署应用程序时会遇到异常。

enter image description here

我得到的例外:

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}

1 个答案:

答案 0 :(得分:0)

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

问题的关键在于您的网址缺少应用程序的上下文路径(在您引用的教程中为uses-sdk)。

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

  

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

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