Jboss下运行RestEasy报错

时间:2021-11-11 07:28:20

使用Jboss运行RestEasy项目时报错如下错误:

JBoss Bootstrap Environment

JBOSS_HOME: /usr/jboss/jboss-as-7.1.1.Final

JAVA: /usr/java/jdk1.7.0_79/bin/java

JAVA_OPTS: -server -XX:+UseCompressedOops -XX:+TieredCompilation -Xms64m -Xmx512m -XX:MaxPermSize=256m -Djava.net.preferIPv4Stack=true -Dorg.jboss.resolver.warning=true -Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000 -Djboss.modules.system.pkgs=org.jboss.byteman -Djava.awt.headless=true -Djboss.server.default.config=standalone.xml

=========================================================================
19:36:07,811 INFO  [org.jboss.modules] JBoss Modules version 1.1.1.GA
19:36:07,911 INFO [org.jboss.msc] JBoss MSC version 1.0.2.GA
19:36:07,940 INFO [org.jboss.as] JBAS015899: JBoss AS 7.1.1.Final "Brontes" starting
19:36:08,476 INFO [org.xnio] XNIO Version 3.0.3.GA
19:36:08,481 INFO [org.jboss.as.server] JBAS015888: Creating http management service using socket-binding (management-http)
19:36:08,488 INFO [org.xnio.nio] XNIO NIO Implementation Version 3.0.3.GA
19:36:08,494 INFO [org.jboss.remoting] JBoss Remoting version 3.2.3.GA
19:36:08,525 INFO [org.jboss.as.logging] JBAS011502: Removing bootstrap log handlers
19:36:08,528 INFO [org.jboss.as.configadmin] (ServerService Thread Pool -- 26) JBAS016200: Activating ConfigAdmin Subsystem
19:36:08,536 INFO [org.jboss.as.clustering.infinispan] (ServerService Thread Pool -- 31) JBAS010280: Activating Infinispan subsystem.
19:36:08,583 INFO [org.jboss.as.naming] (ServerService Thread Pool -- 38) JBAS011800: Activating Naming Subsystem
19:36:08,586 INFO [org.jboss.as.security] (ServerService Thread Pool -- 44) JBAS013101: Activating Security Subsystem
19:36:08,602 INFO [org.jboss.as.osgi] (ServerService Thread Pool -- 39) JBAS011940: Activating OSGi Subsystem
19:36:08,607 INFO [org.jboss.as.security] (MSC service thread 1-5) JBAS013100: Current PicketBox version=4.0.7.Final
19:36:08,629 INFO [org.jboss.as.connector] (MSC service thread 1-6) JBAS010408: Starting JCA Subsystem (JBoss IronJacamar 1.0.9.Final)
19:36:08,653 INFO [org.jboss.as.webservices] (ServerService Thread Pool -- 48) JBAS015537: Activating WebServices Extension
19:36:08,852 INFO [org.jboss.as.connector.subsystems.datasources] (ServerService Thread Pool -- 27) JBAS010403: Deploying JDBC-compliant driver class org.h2.Driver (version 1.3)
19:36:08,872 INFO [org.jboss.as.naming] (MSC service thread 1-6) JBAS011802: Starting Naming Service
19:36:08,872 INFO [org.jboss.ws.common.management.AbstractServerConfig] (MSC service thread 1-2) JBoss Web Services - Stack CXF Server 4.0.2.GA
19:36:08,874 INFO [org.jboss.as.mail.extension] (MSC service thread 1-3) JBAS015400: Bound mail session [java:jboss/mail/Default]
19:36:09,075 INFO [org.apache.coyote.http11.Http11Protocol] (MSC service thread 1-6) Starting Coyote HTTP/1.1 on http--127.0.0.1-8080
19:36:09,279 INFO [org.jboss.as.remoting] (MSC service thread 1-8) JBAS017100: Listening on /127.0.0.1:9999
19:36:09,281 INFO [org.jboss.as.remoting] (MSC service thread 1-1) JBAS017100: Listening on /127.0.0.1:4447
19:36:09,284 INFO [org.jboss.as.server.deployment.scanner] (MSC service thread 1-6) JBAS015012: Started FileSystemDeploymentService for directory /usr/jboss/jboss-as-7.1.1.Final/standalone/deployments
19:36:09,362 INFO [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-4) JBAS010400: Bound data source [java:jboss/datasources/ExampleDS]
19:36:09,374 INFO [org.jboss.as.server.deployment] (MSC service thread 1-4) JBAS015876: Starting deployment of "restEasyTest.war"
19:36:09,753 WARN [org.jboss.jaxrs] (MSC service thread 1-7) JBAS011204: resteasy.scan found and ignored in web.xml. This is not necessary, as Resteasy will use the container integration in the JAX-RS 1.1 specification in section 2.3.2
19:36:09,912 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/restEasyTest]] (MSC service thread 1-5) Exception sending context initialized event to listener instance of class org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap: java.lang.RuntimeException: Unable to find a public constructor for class org.jboss.resteasy.core.AsynchronousDispatcher
at org.jboss.resteasy.plugins.server.resourcefactory.POJOResourceFactory.registered(POJOResourceFactory.java:35) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.core.ResourceMethodRegistry.addResourceFactory(ResourceMethodRegistry.java:121) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.core.ResourceMethodRegistry.addResourceFactory(ResourceMethodRegistry.java:107) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.core.ResourceMethodRegistry.addResourceFactory(ResourceMethodRegistry.java:84) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.core.ResourceMethodRegistry.addPerRequestResource(ResourceMethodRegistry.java:73) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.spi.ResteasyDeployment.registration(ResteasyDeployment.java:367) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.spi.ResteasyDeployment.start(ResteasyDeployment.java:225) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap.contextInitialized(ResteasyBootstrap.java:28) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.apache.catalina.core.StandardContext.contextListenerStart(StandardContext.java:3392) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardContext.start(StandardContext.java:3850) [jbossweb-7.0.13.Final.jar:]
at org.jboss.as.web.deployment.WebDeploymentService.start(WebDeploymentService.java:90) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_79]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_79]
at java.lang.Thread.run(Thread.java:745) [rt.jar:1.7.0_79]

19:36:09,944 ERROR [org.apache.catalina.core.StandardContext] (MSC service thread 1-5) Error listenerStart
19:36:09,944 ERROR [org.apache.catalina.core.StandardContext] (MSC service thread 1-5) Context [/restEasyTest] startup failed due to previous errors
19:36:09,955 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-5) MSC00001: Failed to start service jboss.web.deployment.default-host./restEasyTest: org.jboss.msc.service.StartException in service jboss.web.deployment.default-host./restEasyTest: JBAS018040: Failed to start context
at org.jboss.as.web.deployment.WebDeploymentService.start(WebDeploymentService.java:95)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_79]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_79]
at java.lang.Thread.run(Thread.java:745) [rt.jar:1.7.0_79]

19:36:09,957 INFO [org.jboss.as] (MSC service thread 1-5) JBAS015951: Admin console listening on http://127.0.0.1:9990
19:36:09,957 ERROR [org.jboss.as] (MSC service thread 1-5) JBAS015875: JBoss AS 7.1.1.Final "Brontes" started (with errors) in 2306ms - Started 174 of 253 services (2 services failed or missing dependencies, 76 services are passive or on-demand)
19:36:10,159 INFO [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS015870: Deploy of deployment "restEasyTest.war" was rolled back with failure message {"JBAS014671: Failed services" => {"jboss.web.deployment.default-host./restEasyTest" => "org.jboss.msc.service.StartException in service jboss.web.deployment.default-host./restEasyTest: JBAS018040: Failed to start context"}}
19:36:10,167 INFO [org.jboss.as.server.deployment] (MSC service thread 1-8) JBAS015877: Stopped deployment restEasyTest.war in 8ms
19:36:10,168 INFO [org.jboss.as.controller] (DeploymentScanner-threads - 2) JBAS014774: Service status report
JBAS014777: Services which failed to start: service jboss.web.deployment.default-host./restEasyTest: org.jboss.msc.service.StartException in service jboss.web.deployment.default-host./restEasyTest: JBAS018040: Failed to start context

19:36:10,169 ERROR [org.jboss.as.server.deployment.scanner] (DeploymentScanner-threads - 1) {"JBAS014653: Composite operation failed and was rolled back. Steps that failed:" => {"Operation step-2" => {"JBAS014671: Failed services" => {"jboss.web.deployment.default-host./restEasyTest" => "org.jboss.msc.service.StartException in service jboss.web.deployment.default-host./restEasyTest: JBAS018040: Failed to start context"}}}}
^C19:42:48,561 INFO [org.jboss.as.logging] JBAS011503: Restored bootstrap log handlers
19:42:48,565 INFO [org.apache.coyote.http11.Http11Protocol] Pausing Coyote HTTP/1.1 on http--127.0.0.1-8080
19:42:48,565 INFO [org.apache.coyote.http11.Http11Protocol] Stopping Coyote HTTP/1.1 on http--127.0.0.1-8080
19:42:48,567 INFO [com.arjuna.ats.jbossatx] ARJUNA032018: Destroying TransactionManagerService
19:42:48,567 INFO [com.arjuna.ats.jbossatx] ARJUNA032014: Stopping transaction recovery manager
19:42:48,581 INFO [org.jboss.as] JBAS015950: JBoss AS 7.1.1.Final "Brontes" stopped in 40ms

项目的源码如下:
此项目为Maven webapp项目:
MessageRestService.java

package com.mk.rest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

@Path("/message")
public class MessageRestService {
public MessageRestService(){}
@GET
@Path("/{param}")
public Response printMessage(@PathParam("param") String msg) {

String result = "Restful example : " + msg;

return Response.status(200).entity(result).build();

}
}

pom.xml配置resteasy依赖

  <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>2.2.1.GA</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>JBoss repository</id>
<url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
</repository>
</repositories>

web.xml配置拦截器

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >


<web-app>
<display-name>Archetype Created Web Application</display-name>

<servlet>
<servlet-name>resteasy-servlet</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>resteasy-servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

上面这些代码是从网上的RestEasy扒下来然后直接运行就报最上面的错,找了很久发现报错的原因是在web.xml配置的servlet中的HttpServletDispatcher的问题。解决办法是在servelt-class中加入如下信息:

        <init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.mk.rest.RestApplication</param-value>
</init-param>

并且在com.mk.rest中新增类RestApplication.java

package com.mk.rest;

import java.util.HashSet;
import java.util.Set;

public class RestApplication extends javax.ws.rs.core.Application{

private Set<Object> singletons = new HashSet<Object>();

public RestApplication () {
singletons.add(new MessageRestService());
}

@Override
public Set<Object> getSingletons() {
return singletons;
}
}

这样想应该就OK了。然后编译运行,发现又报一个错。

19:55:16,243 ERROR [org.jboss.as.controller.management-operation] (DeploymentScanner-threads - 2) Operation ("add") failed - address: ([("deployment" => "restEasyTest.war")]) - failure description: "JBAS014803: Duplicate resource [(\"deployment\" => \"restEasyTest.war\")]"

然后找啊找啊找。。

在jboss运行war包后,在同一目录下会自动生成一个restEasyTest.war.deployed
这个文件。把整个文件删除之后,再运行。。。。OK了~
Jboss下运行RestEasy报错
说明: 我是把war包放在jboss中运行的。

第一次写这类博客,也第一次接触resteasy,有错的地方望大牛们指正,可能逻辑有点乱。望包含