如何使用wildfly访问rest资源?

ma8fv8wu  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(440)

嗨,我正在开发一个restapi与intellij遵循官方文件。
但我得到一个404找不到错误,而访问资源。。。我仍然可以访问index.jsp页面。我遵循了de doc的每一步,但我用的是wildfly而不是glassfish。
以下是资源:

  1. @Path("devices")
  2. public class DevicesResource {
  3. @GET
  4. @Produces(MediaType.TEXT_PLAIN)
  5. public String getAllDevices() {
  6. return "Hello world from thingsplay.";
  7. }
  8. }

下面是应用程序子类:

  1. @ApplicationPath("/api/v2.0")
  2. public class ApplicationConfig extends Application {
  3. @Override
  4. public Set<Class<?>> getClasses() {
  5. Set<Class<?>> resources = new HashSet<>();
  6. addResourceClasses(resources);
  7. return resources;
  8. }
  9. public void addResourceClasses(Set<Class<?>> resources) {
  10. resources.add(be.thingsplay.resources.DevicesResource.class);
  11. }
  12. }

所以当我进入http://localhost:8080/api/v2.0我得到索引页,但当我尝试访问http://localhost:8080/api/v2.0/devices我得到一个404 not found错误。
当我看到这件文物时,我注意到泽西岛图书馆不在战争中。但是,当我尝试将它添加到war中时,当我尝试部署应用程序时会出现异常。

我得到的例外:

  1. 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")
  2. 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)
  3. 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
  4. at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:85)
  5. at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
  6. at java.util.concurrent.FutureTask.run(FutureTask.java:266)
  7. at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
  8. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
  9. at java.lang.Thread.run(Thread.java:745)
  10. at org.jboss.threads.JBossThread.run(JBossThread.java:320)
  11. Caused by: java.lang.NoClassDefFoundError: com/google/common/base/Function
  12. at org.glassfish.jersey.internal.ServiceFinder.<clinit>(ServiceFinder.java:165)
  13. at org.glassfish.jersey.servlet.internal.ServletContainerProviderFactory.getAllServletContainerProviders(ServletContainerProviderFactory.java:66)
  14. at org.glassfish.jersey.servlet.init.JerseyServletContainerInitializer.onStartup(JerseyServletContainerInitializer.java:132)
  15. at io.undertow.servlet.core.DeploymentManagerImpl$1.call(DeploymentManagerImpl.java:186)
  16. at io.undertow.servlet.core.DeploymentManagerImpl$1.call(DeploymentManagerImpl.java:171)
  17. at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:42)
  18. at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
  19. at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
  20. at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
  21. at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
  22. at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
  23. at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
  24. at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:234)
  25. at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:100)
  26. at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:82)
  27. ... 6 more
  28. Caused by: java.lang.ClassNotFoundException: com.google.common.base.Function from [Module "deployment.apirest_war_exploded.war:main" from Service Module Loader]
  29. at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:198)
  30. at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:363)
  31. at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:351)
  32. at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:93)
  33. ... 21 more
  34. 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: {
  35. "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
  36. Caused by: java.lang.NoClassDefFoundError: com/google/common/base/Function
  37. Caused by: java.lang.ClassNotFoundException: com.google.common.base.Function from [Module \"deployment.apirest_war_exploded.war:main\" from Service Module Loader]"},
  38. "WFLYCTL0412: Required services that are not installed:" => ["jboss.undertow.deployment.default-server.default-host./apirest_war_exploded"],
  39. "WFLYCTL0180: Services with missing/unavailable dependencies" => undefined
  40. }
  41. 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:
  42. {
  43. "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
  44. Caused by: java.lang.NoClassDefFoundError: com/google/common/base/Function
  45. Caused by: java.lang.ClassNotFoundException: com.google.common.base.Function from [Module \"deployment.apirest_war_exploded.war:main\" from Service Module Loader]"},
  46. "WFLYCTL0412: Required services that are not installed:" => ["jboss.undertow.deployment.default-server.default-host./apirest_war_exploded"],
  47. "WFLYCTL0180: Services with missing/unavailable dependencies" => undefined
  48. }
  49. 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
  50. 14:57:24,208 INFO [org.jboss.as.controller] (management-handler-thread - 20) WFLYCTL0183: Service status report
  51. WFLYCTL0184: New missing/unsatisfied dependencies:
  52. service jboss.undertow.deployment.default-server.default-host./apirest_war_exploded (missing) dependents: [service jboss.deployment.unit."apirest_war_exploded.war".deploymentCompleteService]
  53. WFLYCTL0186: Services which failed to start: service jboss.undertow.deployment.default-server.default-host./apirest_war_exploded
  54. [2017-07-14 02:57:24,302] Artifact apirest:war exploded: Error during artifact deployment. See server log for details.
  55. [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
  56. Caused by: java.lang.NoClassDefFoundError: com/google/common/base/Function
  57. 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}
iklwldmw

iklwldmw1#

首先,您不需要向应用程序添加任何rest实现jar(比如jersey),因为javaee7服务器(比如wildfly和glassfish)是免费的。
问题的关键在于,url缺少应用程序的上下文路径(即 rest_glassfish_hello_world_war_exploded 在您引用的教程中)。
即,正确的服务url应类似于:
http://localhost:8080/rest\u glassfish\u hello\u world\u war\u exploded/api/v2.0/devices
哪里 rest_glassfish_hello_world_war_exploded 如果实际应用程序名称不同,则应替换为实际应用程序名称。

相关问题