我正在尝试使用 RESTEasy 和 Weld 进行注入(inject)来制作 REST web 服务。当包部署在 wildfly 上时,我没有问题,但是在尝试测试服务时,我收到以下错误。
我不知道如何解决这个问题。
有没有人可以帮助我?
21:54:17,066 ERROR [io.undertow.request] (default task-19) UT005023: Exception handling request to /cdi-test/hello: org.jboss.resteasy.spi.UnhandledException: org.jboss.weld.exceptions.IllegalArgumentException: WELD-001456: Argument resolvedBean must not be null
at org.jboss.resteasy.core.ExceptionHandler.handleApplicationException(ExceptionHandler.java:76) [resteasy-jaxrs-3.0.8.Final.jar:]
at org.jboss.resteasy.core.ExceptionHandler.handleException(ExceptionHandler.java:212) [resteasy-jaxrs-3.0.8.Final.jar:]
at org.jboss.resteasy.core.SynchronousDispatcher.writeException(SynchronousDispatcher.java:149) [resteasy-jaxrs-3.0.8.Final.jar:]
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:372) [resteasy-jaxrs-3.0.8.Final.jar:]
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:179) [resteasy-jaxrs-3.0.8.Final.jar:]
at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:220) [resteasy-jaxrs-3.0.8.Final.jar:]
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56) [resteasy-jaxrs-3.0.8.Final.jar:]
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51) [resteasy-jaxrs-3.0.8.Final.jar:]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) [jboss-servlet-api_3.1_spec-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:61) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:113) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:56) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:45) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:61) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:70) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:240) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:227) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:73) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:146) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.server.Connectors.executeRootHandler(Connectors.java:177) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:727) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [rt.jar:1.8.0_20]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [rt.jar:1.8.0_20]
at java.lang.Thread.run(Thread.java:745) [rt.jar:1.8.0_20]
Caused by: org.jboss.weld.exceptions.IllegalArgumentException: WELD-001456: Argument resolvedBean must not be null
at org.jboss.weld.util.Preconditions.checkArgumentNotNull(Preconditions.java:40) [weld-core-impl-2.1.2.Final.jar:2014-01-09 09:23]
at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:766) [weld-core-impl-2.1.2.Final.jar:2014-01-09 09:23]
at org.jboss.weld.injection.ParameterInjectionPointImpl.getValueToInject(ParameterInjectionPointImpl.java:76) [weld-core-impl-2.1.2.Final.jar:2014-01-09 09:23]
at org.jboss.weld.injection.ConstructorInjectionPoint.getParameterValues(ConstructorInjectionPoint.java:138) [weld-core-impl-2.1.2.Final.jar:2014-01-09 09:23]
at org.jboss.weld.injection.ConstructorInjectionPoint.newInstance(ConstructorInjectionPoint.java:68) [weld-core-impl-2.1.2.Final.jar:2014-01-09 09:23]
at org.jboss.weld.injection.producer.AbstractInstantiator.newInstance(AbstractInstantiator.java:28) [weld-core-impl-2.1.2.Final.jar:2014-01-09 09:23]
at org.jboss.weld.injection.producer.BasicInjectionTarget.produce(BasicInjectionTarget.java:85) [weld-core-impl-2.1.2.Final.jar:2014-01-09 09:23]
at org.jboss.weld.injection.producer.BeanInjectionTarget.produce(BeanInjectionTarget.java:183) [weld-core-impl-2.1.2.Final.jar:2014-01-09 09:23]
at org.hibernate.validator.internal.cdi.DestructibleBeanInstance.createAndInjectBeans(DestructibleBeanInstance.java:53) [hibernate-validator-cdi-5.1.0.Final.jar:5.1.0.Final]
at org.hibernate.validator.internal.cdi.DestructibleBeanInstance.<init>(DestructibleBeanInstance.java:33) [hibernate-validator-cdi-5.1.0.Final.jar:5.1.0.Final]
at org.hibernate.validator.internal.cdi.ValidatorFactoryBean.createInstance(ValidatorFactoryBean.java:206) [hibernate-validator-cdi-5.1.0.Final.jar:5.1.0.Final]
at org.hibernate.validator.internal.cdi.ValidatorFactoryBean.createConstraintValidatorFactory(ValidatorFactoryBean.java:192) [hibernate-validator-cdi-5.1.0.Final.jar:5.1.0.Final]
at org.hibernate.validator.internal.cdi.ValidatorFactoryBean.create(ValidatorFactoryBean.java:119) [hibernate-validator-cdi-5.1.0.Final.jar:5.1.0.Final]
at org.hibernate.validator.internal.cdi.ValidatorFactoryBean.create(ValidatorFactoryBean.java:52) [hibernate-validator-cdi-5.1.0.Final.jar:5.1.0.Final]
at org.jboss.weld.context.AbstractContext.get(AbstractContext.java:96) [weld-core-impl-2.1.2.Final.jar:2014-01-09 09:23]
at org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:98) [weld-core-impl-2.1.2.Final.jar:2014-01-09 09:23]
at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:99) [weld-core-impl-2.1.2.Final.jar:2014-01-09 09:23]
at org.jboss.weld.proxies.ValidatorFactory$627848863$Proxy$_$$_WeldClientProxy.getMessageInterpolator(Unknown Source) [weld-core-impl-2.1.2.Final.jar:]
at org.jboss.as.ee.beanvalidation.LazyValidatorFactory.getMessageInterpolator(LazyValidatorFactory.java:101)
at org.jboss.resteasy.plugins.validation.GeneralValidatorImpl.getValidator(GeneralValidatorImpl.java:559) [resteasy-validator-provider-11-3.0.8.Final.jar:]
at org.jboss.resteasy.plugins.validation.GeneralValidatorImpl.validate(GeneralValidatorImpl.java:69) [resteasy-validator-provider-11-3.0.8.Final.jar:]
at org.jboss.resteasy.cdi.JaxrsInjectionTarget.validate(JaxrsInjectionTarget.java:108) [resteasy-cdi-3.0.8.Final.jar:]
at org.jboss.resteasy.cdi.JaxrsInjectionTarget.inject(JaxrsInjectionTarget.java:69) [resteasy-cdi-3.0.8.Final.jar:]
at org.jboss.weld.bean.ManagedBean.create(ManagedBean.java:150) [weld-core-impl-2.1.2.Final.jar:2014-01-09 09:23]
at org.jboss.weld.context.AbstractContext.get(AbstractContext.java:96) [weld-core-impl-2.1.2.Final.jar:2014-01-09 09:23]
at org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:98) [weld-core-impl-2.1.2.Final.jar:2014-01-09 09:23]
at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:78) [weld-core-impl-2.1.2.Final.jar:2014-01-09 09:23]
at com.test.service.HelloWorld$Proxy$_$$_WeldClientProxy.getHelloWorldJSON(Unknown Source) [classes:]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.8.0_20]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) [rt.jar:1.8.0_20]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.8.0_20]
at java.lang.reflect.Method.invoke(Method.java:483) [rt.jar:1.8.0_20]
at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:137) [resteasy-jaxrs-3.0.8.Final.jar:]
at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:296) [resteasy-jaxrs-3.0.8.Final.jar:]
at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:250) [resteasy-jaxrs-3.0.8.Final.jar:]
at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:237) [resteasy-jaxrs-3.0.8.Final.jar:]
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:356) [resteasy-jaxrs-3.0.8.Final.jar:]
... 31 more
我的项目文件,
package com.test.service;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("/")
public class RESTConfig extends Application {
}
package com.test.service;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import javax.enterprise.context.RequestScoped;
@Path("/hello")
@RequestScoped
public class HelloWorld {
@Inject
HelloService helloService;
@GET
@Produces({ "application/json" })
public Response getHelloWorldJSON() {
String res = helloService.createHelloMessage("World");
return Response.ok(res).build();
}
}
package com.test.service;
import javax.enterprise.context.RequestScoped;
@RequestScoped
public class HelloService {
String createHelloMessage(String name) {
return "Hello " + name + "!";
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test.service</groupId>
<artifactId>cdi-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.jboss.weld</groupId>
<artifactId>weld-core</artifactId>
<version>2.2.4.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.0.7.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>jaxrs-api</artifactId>
<version>3.0.7.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson2-provider</artifactId>
<version>3.0.7.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-cdi</artifactId>
<version>3.0.7.Final</version>
</dependency>
</dependencies>
</project>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="all">
</beans>
最佳答案
如果您不使用特定的weld 或resteasy 功能,则不应添加weld 和resteasy 依赖项。如果您这样做,您应该将这些依赖项放在 provided
范围内。否则它将被打包在你的 WAR 中。
但是查看您的代码,您似乎没有使用特定的weld 或resteasy 功能。在这种情况下,我推荐的只是为 Java EE api 添加一个依赖项,而不是其他任何东西:
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
Wildfly 将在运行时提供所有实现。
关于jax-rs - 使用 RESTEasy、Weld 和 Wildfly 失败的 @Inject 对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25712344/