在 WildFly 上部署 java 网络应用程序时出现错误 404
Error 404 while deploying a java web app on WildFly
我正在学习关于
https://www.youtube.com/watch?v=dmMdLW92hBA.
在项目中引入 index.xhtml
的最后一部分之前,一切都很顺利。
我在浏览器中收到 404 错误,在 wildfly 控制台日志中收到以下错误:
18:24:52,128 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 64) MSC000001: Failed to start service jboss.undertow.deployment.default-server.default-host./RestExample: org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./RestExample: org.jboss.weld.context.ContextNotActiveException: WELD-001303: No active contexts for scope type javax.enterprise.context.RequestScoped
at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.run(UndertowDeploymentService.java:85)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
at org.jboss.threads.JBossThread.run(JBossThread.java:320)
Caused by: org.jboss.weld.context.ContextNotActiveException: WELD-001303: No active contexts for scope type javax.enterprise.context.RequestScoped
at org.jboss.weld.manager.BeanManagerImpl.getContext(BeanManagerImpl.java:689)
at org.jboss.weld.bean.ContextualInstanceStrategy$DefaultContextualInstanceStrategy.getIfExists(ContextualInstanceStrategy.java:90)
at org.jboss.weld.bean.ContextualInstanceStrategy$CachingContextualInstanceStrategy.getIfExists(ContextualInstanceStrategy.java:165)
at org.jboss.weld.bean.ContextualInstance.getIfExists(ContextualInstance.java:63)
at org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:83)
at org.jboss.weld.bean.proxy.ProxyMethodHandler.getInstance(ProxyMethodHandler.java:125)
at org.jboss.weld.proxies.RestService$Proxy$_$$_WeldClientProxy.getClasses(Unknown Source)
at org.jboss.resteasy.spi.ResteasyDeployment.processApplication(ResteasyDeployment.java:490)
at org.jboss.resteasy.spi.ResteasyDeployment.registration(ResteasyDeployment.java:336)
at org.jboss.resteasy.spi.ResteasyDeployment.start(ResteasyDeployment.java:250)
at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.init(ServletContainerDispatcher.java:113)
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.init(HttpServletDispatcher.java:36)
at io.undertow.servlet.core.LifecyleInterceptorInvocation.proceed(LifecyleInterceptorInvocation.java:117)
at org.wildfly.extension.undertow.security.RunAsLifecycleInterceptor.init(RunAsLifecycleInterceptor.java:78)
at io.undertow.servlet.core.LifecyleInterceptorInvocation.proceed(LifecyleInterceptorInvocation.java:103)
at io.undertow.servlet.core.ManagedServlet$DefaultInstanceStrategy.start(ManagedServlet.java:250)
at io.undertow.servlet.core.ManagedServlet.createServlet(ManagedServlet.java:133)
at io.undertow.servlet.core.DeploymentManagerImpl.call(DeploymentManagerImpl.java:546)
at io.undertow.servlet.core.DeploymentManagerImpl.call(DeploymentManagerImpl.java:517)
at io.undertow.servlet.core.ServletRequestContextThreadSetupAction.call(ServletRequestContextThreadSetupAction.java:42)
at io.undertow.servlet.core.ContextClassLoaderSetupAction.call(ContextClassLoaderSetupAction.java:43)
at io.undertow.servlet.api.LegacyThreadSetupActionWrapper.call(LegacyThreadSetupActionWrapper.java:44)
at io.undertow.servlet.api.LegacyThreadSetupActionWrapper.call(LegacyThreadSetupActionWrapper.java:44)
at io.undertow.servlet.api.LegacyThreadSetupActionWrapper.call(LegacyThreadSetupActionWrapper.java:44)
at io.undertow.servlet.api.LegacyThreadSetupActionWrapper.call(LegacyThreadSetupActionWrapper.java:44)
at io.undertow.servlet.api.LegacyThreadSetupActionWrapper.call(LegacyThreadSetupActionWrapper.java:44)
at io.undertow.servlet.core.DeploymentManagerImpl.start(DeploymentManagerImpl.java:559)
at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:101)
at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.run(UndertowDeploymentService.java:82)
... 6 more
18:24:52,133 ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) WFLYCTL0013: Operation ("deploy") failed - address: ([("deployment" => "RestExample.war")]) - failure description: {
"WFLYCTL0080: Failed services" => {"jboss.undertow.deployment.default-server.default-host./RestExample" => "org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./RestExample: org.jboss.weld.context.ContextNotActiveException: WELD-001303: No active contexts for scope type javax.enterprise.context.RequestScoped
Caused by: org.jboss.weld.context.ContextNotActiveException: WELD-001303: No active contexts for scope type javax.enterprise.context.RequestScoped"},
"WFLYCTL0412: Required services that are not installed:" => ["jboss.undertow.deployment.default-server.default-host./RestExample"],
"WFLYCTL0180: Services with missing/unavailable dependencies" => undefined
}
WFLYCTL0184: New missing/unsatisfied dependencies:
service jboss.deployment.unit."RestExample.war".component."com.sun.faces.config.ConfigureListener".START (missing) dependents: [service jboss.deployment.unit."RestExample.war".deploymentCompleteService]
service jboss.deployment.unit."RestExample.war".component."javax.faces.webapp.FacesServlet".START (missing) dependents: [service jboss.deployment.unit."RestExample.war".deploymentCompleteService]
service jboss.deployment.unit."RestExample.war".component."javax.faces.webapp.FacetTag".START (missing) dependents: [service jboss.deployment.unit."RestExample.war".deploymentCompleteService]
service jboss.deployment.unit."RestExample.war".component."managed-bean.RestClient".START (missing) dependents: [service jboss.deployment.unit."RestExample.war".deploymentCompleteService]
service jboss.deployment.unit."RestExample.war".component."org.jboss.weld.servlet.WeldInitialListener".START (missing) dependents: [service jboss.deployment.unit."RestExample.war".deploymentCompleteService]
WFLYCTL0186: Services which failed to start: service jboss.undertow.deployment.default-server.default-host./RestExample
我的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>RestExample</display-name>
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
</web-app>
我创建了一个空 beans.xml 就像在教程中一样。
我的 index.xhtml 的内容:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
</h:head>
<h:body>
<h:form>
<h:commandButton value="Call Webservice" action="#{restClient.call()}" />
</h:form>
</h:body>
</html>
RestClient.java:
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
@ApplicationScoped
@ManagedBean
public class RestClient {
public void call() {
Client client = ClientBuilder.newClient();
SimpleObject o = client.target("http://localhost:8080/RestExample/resources/MyRestService/object").request().get(SimpleObject.class);
System.out.println(o);
}
}
RestService.java:
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path("/MyRestService")
@ApplicationPath("/resources")
public class RestService extends Application {
// http://localhost:8080/RestExample/resources/MyRestService/sayHello
@GET
@Path("/sayHello")
public String getHelloMsg() {
return "Hello World";
}
@GET
@Path("/echo")
public Response getEchoMsg(@QueryParam("message") String msg) {
return Response.ok("Your message was: " + msg).build();
}
@GET
@Path("/object")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public SimpleObject getObject() {
return new SimpleObject(1, "Test");
}
}
SimpleObject.java:
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class SimpleObject {
private int id;
private String name;
public SimpleObject() {
}
public SimpleObject(int id, String name) {
super();
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
我下载了代码库以及 WildFly 10.1.0.Final
服务器。我也遇到了同样的问题。
根本原因: 注释 @ApplicationPath
和 @Path
用于造成问题的相同 class。
@ApplicationPath
用于为整个应用程序提供基本 URI 路径。
@Path
用于自定义控制器 class 或 class 方法的单独 URI 路径。
这 RestService.java
class 造成了问题。
修改RestService.java
(从class中删除@ApplicationPath
):
@Path("/MyRestService")
public class RestService {
// http://localhost:8080/RestExample/resources/MyRestService/sayHello
@GET
@Path("/sayHello")
public String getHelloMsg() {
return "Hello World";
}
@GET
@Path("/echo")
public Response getEchoMsg(@QueryParam("message") String msg) {
return Response.ok("Your message was: " + msg).build();
}
@GET
@Path("/object")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public SimpleObject getObject() {
return new SimpleObject(1, "Test");
}
}
我添加了一个新的 class RestApp.java
。代码:
@ApplicationPath("/resources")
public class RestApp extends Application {
}
更新的项目结构:
Wildfly 日志(部署成功):
21:05:09,997 INFO [org.jboss.as.server.deployment.scanner] (MSC service thread 1-7) WFLYDS0013: Started FileSystemDeploymentService for directory C:\Users\Anish\Downloads\wildfly-10.1.0.Final\wildfly-10.1.0.Final\standalone\deployments
21:05:10,157 INFO [org.jboss.as.server.deployment] (MSC service thread 1-7) WFLYSRV0027: Starting deployment of "RestExample.war" (runtime-name: "RestExample.war")
21:05:10,312 INFO [org.infinispan.factories.GlobalComponentRegistry] (MSC service thread 1-1) ISPN000128: Infinispan version: Infinispan 'Chakra' 8.2.4.Final
21:05:10,432 INFO [org.infinispan.configuration.cache.EvictionConfigurationBuilder] (ServerService Thread Pool -- 63) ISPN000152: Passivation configured without an eviction policy being selected. Only manually evicted entities will be passivated.
21:05:10,434 INFO [org.infinispan.configuration.cache.EvictionConfigurationBuilder] (ServerService Thread Pool -- 60) ISPN000152: Passivation configured without an eviction policy being selected. Only manually evicted entities will be passivated.
21:05:10,473 INFO [org.infinispan.configuration.cache.EvictionConfigurationBuilder] (ServerService Thread Pool -- 60) ISPN000152: Passivation configured without an eviction policy being selected. Only manually evicted entities will be passivated.
21:05:10,476 INFO [org.infinispan.configuration.cache.EvictionConfigurationBuilder] (ServerService Thread Pool -- 63) ISPN000152: Passivation configured without an eviction policy being selected. Only manually evicted entities will be passivated.
21:05:10,431 INFO [org.infinispan.configuration.cache.EvictionConfigurationBuilder] (ServerService Thread Pool -- 65) ISPN000152: Passivation configured without an eviction policy being selected. Only manually evicted entities will be passivated.
21:05:10,512 INFO [org.infinispan.configuration.cache.EvictionConfigurationBuilder] (ServerService Thread Pool -- 65) ISPN000152: Passivation configured without an eviction policy being selected. Only manually evicted entities will be passivated.
21:05:12,754 INFO [org.wildfly.extension.undertow] (MSC service thread 1-4) WFLYUT0006: Undertow HTTP listener default listening on 127.0.0.1:8080
21:05:13,012 INFO [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-2) WFLYJCA0001: Bound data source [java:jboss/datasources/ExampleDS]
21:05:13,434 INFO [org.wildfly.extension.undertow] (MSC service thread 1-6) WFLYUT0006: Undertow HTTPS listener https listening on 127.0.0.1:8443
21:05:13,603 INFO [org.jboss.ws.common.management] (MSC service thread 1-6) JBWS022052: Starting JBossWS 5.1.5.Final (Apache CXF 3.1.6)
21:05:14,003 INFO [org.jboss.weld.deployer] (MSC service thread 1-4) WFLYWELD0003: Processing weld deployment RestExample.war
21:05:14,103 INFO [org.hibernate.validator.internal.util.Version] (MSC service thread 1-4) HV000001: Hibernate Validator 5.2.4.Final
21:05:14,443 INFO [org.jboss.weld.Version] (MSC service thread 1-4) WELD-000900: 2.3.5 (Final)
21:05:15,748 INFO [javax.enterprise.resource.webcontainer.jsf.config] (ServerService Thread Pool -- 59) Initializing Mojarra 2.2.13.SP1 20160303-1204 for context '/RestExample'
21:05:16,915 INFO [org.jboss.resteasy.resteasy_jaxrs.i18n] (ServerService Thread Pool -- 59) RESTEASY002225: Deploying javax.ws.rs.core.Application: class de.dks.ws.RestApp$Proxy$_$$_WeldClientProxy
21:05:17,050 INFO [org.wildfly.extension.undertow] (ServerService Thread Pool -- 59) WFLYUT0021: Registered web context: /RestExample
21:05:17,144 INFO [org.jboss.as.server] (ServerService Thread Pool -- 34) WFLYSRV0010: Deployed "RestExample.war" (runtime-name : "RestExample.war")
21:05:17,304 INFO [org.jboss.as] (Controller Boot Thread) WFLYSRV0060: Http management interface listening on http://127.0.0.1:9990/management
21:05:17,305 INFO [org.jboss.as] (Controller Boot Thread) WFLYSRV0051: Admin console listening on http://127.0.0.1:9990
21:05:17,310 INFO [org.jboss.as] (Controller Boot Thread) WFLYSRV0025: WildFly Full 10.1.0.Final (WildFly Core 2.2.0.Final) started in 13646ms - Started 435 of 683 services (404 services are lazy, passive or on-demand)
截图:
RestClient 的屏幕截图:
通过单击 调用 Web 服务 生成输出:
注意:不要忘记在项目中添加这些 jar javax.faces.api-2.2.jar
和 javax.ws.rs-api-2.0.1.jar
WebContent/WEB-INF/lib
.
我正在学习关于 https://www.youtube.com/watch?v=dmMdLW92hBA.
在项目中引入 index.xhtml
的最后一部分之前,一切都很顺利。
我在浏览器中收到 404 错误,在 wildfly 控制台日志中收到以下错误:
18:24:52,128 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 64) MSC000001: Failed to start service jboss.undertow.deployment.default-server.default-host./RestExample: org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./RestExample: org.jboss.weld.context.ContextNotActiveException: WELD-001303: No active contexts for scope type javax.enterprise.context.RequestScoped
at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.run(UndertowDeploymentService.java:85)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
at org.jboss.threads.JBossThread.run(JBossThread.java:320)
Caused by: org.jboss.weld.context.ContextNotActiveException: WELD-001303: No active contexts for scope type javax.enterprise.context.RequestScoped
at org.jboss.weld.manager.BeanManagerImpl.getContext(BeanManagerImpl.java:689)
at org.jboss.weld.bean.ContextualInstanceStrategy$DefaultContextualInstanceStrategy.getIfExists(ContextualInstanceStrategy.java:90)
at org.jboss.weld.bean.ContextualInstanceStrategy$CachingContextualInstanceStrategy.getIfExists(ContextualInstanceStrategy.java:165)
at org.jboss.weld.bean.ContextualInstance.getIfExists(ContextualInstance.java:63)
at org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:83)
at org.jboss.weld.bean.proxy.ProxyMethodHandler.getInstance(ProxyMethodHandler.java:125)
at org.jboss.weld.proxies.RestService$Proxy$_$$_WeldClientProxy.getClasses(Unknown Source)
at org.jboss.resteasy.spi.ResteasyDeployment.processApplication(ResteasyDeployment.java:490)
at org.jboss.resteasy.spi.ResteasyDeployment.registration(ResteasyDeployment.java:336)
at org.jboss.resteasy.spi.ResteasyDeployment.start(ResteasyDeployment.java:250)
at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.init(ServletContainerDispatcher.java:113)
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.init(HttpServletDispatcher.java:36)
at io.undertow.servlet.core.LifecyleInterceptorInvocation.proceed(LifecyleInterceptorInvocation.java:117)
at org.wildfly.extension.undertow.security.RunAsLifecycleInterceptor.init(RunAsLifecycleInterceptor.java:78)
at io.undertow.servlet.core.LifecyleInterceptorInvocation.proceed(LifecyleInterceptorInvocation.java:103)
at io.undertow.servlet.core.ManagedServlet$DefaultInstanceStrategy.start(ManagedServlet.java:250)
at io.undertow.servlet.core.ManagedServlet.createServlet(ManagedServlet.java:133)
at io.undertow.servlet.core.DeploymentManagerImpl.call(DeploymentManagerImpl.java:546)
at io.undertow.servlet.core.DeploymentManagerImpl.call(DeploymentManagerImpl.java:517)
at io.undertow.servlet.core.ServletRequestContextThreadSetupAction.call(ServletRequestContextThreadSetupAction.java:42)
at io.undertow.servlet.core.ContextClassLoaderSetupAction.call(ContextClassLoaderSetupAction.java:43)
at io.undertow.servlet.api.LegacyThreadSetupActionWrapper.call(LegacyThreadSetupActionWrapper.java:44)
at io.undertow.servlet.api.LegacyThreadSetupActionWrapper.call(LegacyThreadSetupActionWrapper.java:44)
at io.undertow.servlet.api.LegacyThreadSetupActionWrapper.call(LegacyThreadSetupActionWrapper.java:44)
at io.undertow.servlet.api.LegacyThreadSetupActionWrapper.call(LegacyThreadSetupActionWrapper.java:44)
at io.undertow.servlet.api.LegacyThreadSetupActionWrapper.call(LegacyThreadSetupActionWrapper.java:44)
at io.undertow.servlet.core.DeploymentManagerImpl.start(DeploymentManagerImpl.java:559)
at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:101)
at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.run(UndertowDeploymentService.java:82)
... 6 more
18:24:52,133 ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) WFLYCTL0013: Operation ("deploy") failed - address: ([("deployment" => "RestExample.war")]) - failure description: {
"WFLYCTL0080: Failed services" => {"jboss.undertow.deployment.default-server.default-host./RestExample" => "org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./RestExample: org.jboss.weld.context.ContextNotActiveException: WELD-001303: No active contexts for scope type javax.enterprise.context.RequestScoped
Caused by: org.jboss.weld.context.ContextNotActiveException: WELD-001303: No active contexts for scope type javax.enterprise.context.RequestScoped"},
"WFLYCTL0412: Required services that are not installed:" => ["jboss.undertow.deployment.default-server.default-host./RestExample"],
"WFLYCTL0180: Services with missing/unavailable dependencies" => undefined
}
WFLYCTL0184: New missing/unsatisfied dependencies:
service jboss.deployment.unit."RestExample.war".component."com.sun.faces.config.ConfigureListener".START (missing) dependents: [service jboss.deployment.unit."RestExample.war".deploymentCompleteService]
service jboss.deployment.unit."RestExample.war".component."javax.faces.webapp.FacesServlet".START (missing) dependents: [service jboss.deployment.unit."RestExample.war".deploymentCompleteService]
service jboss.deployment.unit."RestExample.war".component."javax.faces.webapp.FacetTag".START (missing) dependents: [service jboss.deployment.unit."RestExample.war".deploymentCompleteService]
service jboss.deployment.unit."RestExample.war".component."managed-bean.RestClient".START (missing) dependents: [service jboss.deployment.unit."RestExample.war".deploymentCompleteService]
service jboss.deployment.unit."RestExample.war".component."org.jboss.weld.servlet.WeldInitialListener".START (missing) dependents: [service jboss.deployment.unit."RestExample.war".deploymentCompleteService]
WFLYCTL0186: Services which failed to start: service jboss.undertow.deployment.default-server.default-host./RestExample
我的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>RestExample</display-name>
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
</web-app>
我创建了一个空 beans.xml 就像在教程中一样。
我的 index.xhtml 的内容:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
</h:head>
<h:body>
<h:form>
<h:commandButton value="Call Webservice" action="#{restClient.call()}" />
</h:form>
</h:body>
</html>
RestClient.java:
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
@ApplicationScoped
@ManagedBean
public class RestClient {
public void call() {
Client client = ClientBuilder.newClient();
SimpleObject o = client.target("http://localhost:8080/RestExample/resources/MyRestService/object").request().get(SimpleObject.class);
System.out.println(o);
}
}
RestService.java:
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path("/MyRestService")
@ApplicationPath("/resources")
public class RestService extends Application {
// http://localhost:8080/RestExample/resources/MyRestService/sayHello
@GET
@Path("/sayHello")
public String getHelloMsg() {
return "Hello World";
}
@GET
@Path("/echo")
public Response getEchoMsg(@QueryParam("message") String msg) {
return Response.ok("Your message was: " + msg).build();
}
@GET
@Path("/object")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public SimpleObject getObject() {
return new SimpleObject(1, "Test");
}
}
SimpleObject.java:
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class SimpleObject {
private int id;
private String name;
public SimpleObject() {
}
public SimpleObject(int id, String name) {
super();
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
我下载了代码库以及 WildFly 10.1.0.Final
服务器。我也遇到了同样的问题。
根本原因: 注释 @ApplicationPath
和 @Path
用于造成问题的相同 class。
@ApplicationPath
用于为整个应用程序提供基本 URI 路径。@Path
用于自定义控制器 class 或 class 方法的单独 URI 路径。
这 RestService.java
class 造成了问题。
修改RestService.java
(从class中删除@ApplicationPath
):
@Path("/MyRestService")
public class RestService {
// http://localhost:8080/RestExample/resources/MyRestService/sayHello
@GET
@Path("/sayHello")
public String getHelloMsg() {
return "Hello World";
}
@GET
@Path("/echo")
public Response getEchoMsg(@QueryParam("message") String msg) {
return Response.ok("Your message was: " + msg).build();
}
@GET
@Path("/object")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public SimpleObject getObject() {
return new SimpleObject(1, "Test");
}
}
我添加了一个新的 class RestApp.java
。代码:
@ApplicationPath("/resources")
public class RestApp extends Application {
}
更新的项目结构:
Wildfly 日志(部署成功):
21:05:09,997 INFO [org.jboss.as.server.deployment.scanner] (MSC service thread 1-7) WFLYDS0013: Started FileSystemDeploymentService for directory C:\Users\Anish\Downloads\wildfly-10.1.0.Final\wildfly-10.1.0.Final\standalone\deployments
21:05:10,157 INFO [org.jboss.as.server.deployment] (MSC service thread 1-7) WFLYSRV0027: Starting deployment of "RestExample.war" (runtime-name: "RestExample.war")
21:05:10,312 INFO [org.infinispan.factories.GlobalComponentRegistry] (MSC service thread 1-1) ISPN000128: Infinispan version: Infinispan 'Chakra' 8.2.4.Final
21:05:10,432 INFO [org.infinispan.configuration.cache.EvictionConfigurationBuilder] (ServerService Thread Pool -- 63) ISPN000152: Passivation configured without an eviction policy being selected. Only manually evicted entities will be passivated.
21:05:10,434 INFO [org.infinispan.configuration.cache.EvictionConfigurationBuilder] (ServerService Thread Pool -- 60) ISPN000152: Passivation configured without an eviction policy being selected. Only manually evicted entities will be passivated.
21:05:10,473 INFO [org.infinispan.configuration.cache.EvictionConfigurationBuilder] (ServerService Thread Pool -- 60) ISPN000152: Passivation configured without an eviction policy being selected. Only manually evicted entities will be passivated.
21:05:10,476 INFO [org.infinispan.configuration.cache.EvictionConfigurationBuilder] (ServerService Thread Pool -- 63) ISPN000152: Passivation configured without an eviction policy being selected. Only manually evicted entities will be passivated.
21:05:10,431 INFO [org.infinispan.configuration.cache.EvictionConfigurationBuilder] (ServerService Thread Pool -- 65) ISPN000152: Passivation configured without an eviction policy being selected. Only manually evicted entities will be passivated.
21:05:10,512 INFO [org.infinispan.configuration.cache.EvictionConfigurationBuilder] (ServerService Thread Pool -- 65) ISPN000152: Passivation configured without an eviction policy being selected. Only manually evicted entities will be passivated.
21:05:12,754 INFO [org.wildfly.extension.undertow] (MSC service thread 1-4) WFLYUT0006: Undertow HTTP listener default listening on 127.0.0.1:8080
21:05:13,012 INFO [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-2) WFLYJCA0001: Bound data source [java:jboss/datasources/ExampleDS]
21:05:13,434 INFO [org.wildfly.extension.undertow] (MSC service thread 1-6) WFLYUT0006: Undertow HTTPS listener https listening on 127.0.0.1:8443
21:05:13,603 INFO [org.jboss.ws.common.management] (MSC service thread 1-6) JBWS022052: Starting JBossWS 5.1.5.Final (Apache CXF 3.1.6)
21:05:14,003 INFO [org.jboss.weld.deployer] (MSC service thread 1-4) WFLYWELD0003: Processing weld deployment RestExample.war
21:05:14,103 INFO [org.hibernate.validator.internal.util.Version] (MSC service thread 1-4) HV000001: Hibernate Validator 5.2.4.Final
21:05:14,443 INFO [org.jboss.weld.Version] (MSC service thread 1-4) WELD-000900: 2.3.5 (Final)
21:05:15,748 INFO [javax.enterprise.resource.webcontainer.jsf.config] (ServerService Thread Pool -- 59) Initializing Mojarra 2.2.13.SP1 20160303-1204 for context '/RestExample'
21:05:16,915 INFO [org.jboss.resteasy.resteasy_jaxrs.i18n] (ServerService Thread Pool -- 59) RESTEASY002225: Deploying javax.ws.rs.core.Application: class de.dks.ws.RestApp$Proxy$_$$_WeldClientProxy
21:05:17,050 INFO [org.wildfly.extension.undertow] (ServerService Thread Pool -- 59) WFLYUT0021: Registered web context: /RestExample
21:05:17,144 INFO [org.jboss.as.server] (ServerService Thread Pool -- 34) WFLYSRV0010: Deployed "RestExample.war" (runtime-name : "RestExample.war")
21:05:17,304 INFO [org.jboss.as] (Controller Boot Thread) WFLYSRV0060: Http management interface listening on http://127.0.0.1:9990/management
21:05:17,305 INFO [org.jboss.as] (Controller Boot Thread) WFLYSRV0051: Admin console listening on http://127.0.0.1:9990
21:05:17,310 INFO [org.jboss.as] (Controller Boot Thread) WFLYSRV0025: WildFly Full 10.1.0.Final (WildFly Core 2.2.0.Final) started in 13646ms - Started 435 of 683 services (404 services are lazy, passive or on-demand)
截图:
RestClient 的屏幕截图:
通过单击 调用 Web 服务 生成输出:
注意:不要忘记在项目中添加这些 jar javax.faces.api-2.2.jar
和 javax.ws.rs-api-2.0.1.jar
WebContent/WEB-INF/lib
.