我是野生蝇群的新手。我能够在swarm中准备一个基本的JAX-RS应用程序,并且像魅力一样工作。但是当我试图将CDI引入我的应用程序时,一切都出错了。 Bean Injection无效!我在我试图调用bean方法的地方得到NULLPointerException。我尝试了所有可能的解决方案,我可以通过网络找到。但没有任何效果。以下是我的pom.xml配置;
<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.uss</groupId>
<artifactId>hatts-swarm</artifactId>
<version>0.0.1-M0</version>
<packaging>war</packaging>
<properties>
<version.wildfly.swarm>2016.12.1</version.wildfly.swarm>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<failOnMissingWebXml>false</failOnMissingWebXml>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>bom-all</artifactId>
<version>${version.wildfly.swarm}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<finalName>jax_cdi</finalName>
<plugins>
<plugin>
<groupId>org.wildfly.swarm</groupId>
<artifactId>wildfly-swarm-plugin</artifactId>
<version>${version.wildfly.swarm}</version>
<executions>
<execution>
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<!-- Java EE 7 dependency -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>jaxrs</artifactId>
<version>2016.12.1</version>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>jaxrs-cdi</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>weld</artifactId>
</dependency>
</dependencies>
</project>
以下是我的主要课程;
public class BootStrap {
/**
* @param args
*/
public static void main(String[] args) {
try {
Swarm swarm = new Swarm(true);
JAXRSArchive deployment = ShrinkWrap.create(JAXRSArchive.class, "jax_cdi_microprofile");
deployment.addClass(RESTEntryManager.class).addClass(HTML5Generator.class)
.addClass(HattsACKTestService.class)
.addAsWebInfResource(new StringAsset(new StringBuilder("<?xml version=\"1.0\"?>")
.append(" <beans bean-discovery-mode=\"all\" version=\"1.1\" ")
.append(" xmlns=\"http://xmlns.jcp.org/xml/ns/javaee\"")
.append(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"")
.append(" xsi:schemaLocation=\"http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd\"/>")
.toString()), "beans.xml")
.addAllDependencies();
swarm.start().deploy(deployment);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
以下是我的JAX-RS资源声明;
@Path("/handshake-test")
@Stateless
public class HattsACKTestService {
@Resource(lookup = "java:jboss/ee/concurrency/executor/default")
private ManagedExecutorService managedExecutorService;
@Inject
private Authenticator authenticator;
public HattsACKTestService() {
// INTENTIONALLY LEFT AS BLANK
}
@GET
public void handshake(@Suspended AsyncResponse asyncResponse) {
/*
* new Thread(() -> {
*
* HTML5Generator html5Generator = new HTML5Generatorg.Builder()
* .setPageTitle("HATTS Handshake Service Response").appendPageBody(
* "H1", "Handshake Initiated...") .build();
*
* asyncResponse.resume(html5Generator.getContentBuilder().toString());
*
* }).start();
*/
/*managedExecutorService.submit(() -> {
asyncResponse.resume("Hi World!!!.. This is Wildfly in CDI !!!");
});*/
asyncResponse.resume(authenticator.authenticate());
}
}
以下是Authenticator bean;
@Stateless
public class Authenticator {
/**
*
*/
public Authenticator() {
// TODO Auto-generated constructor stub
}
public String authenticate() {
return "Authenticated !!";
}
}
甚至没有注射ManagedExectorService
!任何人都可以帮我解决这个问题吗?
在尝试Ken提到的解决方案后,使用最新的错误进行更新
2017-01-08 20:31:01,290 ERROR [stderr] (main) java.lang.reflect.InvocationTargetException
2017-01-08 20:31:01,291 ERROR [stderr] (main) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2017-01-08 20:31:01,291 ERROR [stderr] (main) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
2017-01-08 20:31:01,292 ERROR [stderr] (main) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
2017-01-08 20:31:01,292 ERROR [stderr] (main) at java.lang.reflect.Method.invoke(Method.java:498)
2017-01-08 20:31:01,292 ERROR [stderr] (main) at org.wildfly.swarm.bootstrap.MainInvoker.invoke(MainInvoker.java:37)
2017-01-08 20:31:01,293 ERROR [stderr] (main) at org.wildfly.swarm.bootstrap.Main.run(Main.java:44)
2017-01-08 20:31:01,293 ERROR [stderr] (main) at org.wildfly.swarm.bootstrap.Main.main(Main.java:35)
2017-01-08 20:31:01,294 ERROR [stderr] (main) Caused by: org.wildfly.swarm.container.DeploymentException: WFSWARM0007: Deployment failed: {"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"hatts.war\".WeldStartService" => "org.jboss.msc.service.StartException in service jboss.deployment.unit.\"hatts.war\".WeldStartService: Failed to start service
2017-01-08 20:31:01,294 ERROR [stderr] (main) Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type Authenticator with qualifiers @Default
2017-01-08 20:31:01,294 ERROR [stderr] (main) at injection point [UnbackedAnnotatedField] @Inject private com.uss.hatts.rest.acktest.HattsACKTestService.authenticator
2017-01-08 20:31:01,294 ERROR [stderr] (main) at com.uss.hatts.rest.acktest.HattsACKTestService.authenticator(HattsACKTestService.java:0)
2017-01-08 20:31:01,294 ERROR [stderr] (main) "},"WFLYCTL0412: Required services that are not installed:" => ["jboss.deployment.unit.\"hatts.war\".WeldStartService"],"WFLYCTL0180: Services with missing/unavailable dependencies" => undefined}
2017-01-08 20:31:01,295 ERROR [stderr] (main) at org.wildfly.swarm.container.runtime.RuntimeDeployer.deploy(RuntimeDeployer.java:280)
2017-01-08 20:31:01,295 ERROR [stderr] (main) at org.wildfly.swarm.container.runtime.RuntimeDeployer.deploy(RuntimeDeployer.java:95)
2017-01-08 20:31:01,295 ERROR [stderr] (main) at org.wildfly.swarm.Swarm.deploy(Swarm.java:475)
2017-01-08 20:31:01,295 ERROR [stderr] (main) at org.wildfly.swarm.Swarm.main(Swarm.java:612)
2017-01-08 20:31:01,295 ERROR [stderr] (main) ... 7 more
2017-01-08 20:31:01,296 ERROR [stderr] (main) Exception in thread "main" java.lang.reflect.InvocationTargetException
2017-01-08 20:31:01,296 ERROR [stderr] (main) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2017-01-08 20:31:01,301 ERROR [stderr] (main) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
2017-01-08 20:31:01,303 ERROR [stderr] (main) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
2017-01-08 20:31:01,304 ERROR [stderr] (main) at java.lang.reflect.Method.invoke(Method.java:498)
2017-01-08 20:31:01,304 ERROR [stderr] (main) at org.wildfly.swarm.bootstrap.MainInvoker.invoke(MainInvoker.java:37)
2017-01-08 20:31:01,304 ERROR [stderr] (main) at org.wildfly.swarm.bootstrap.Main.run(Main.java:44)
2017-01-08 20:31:01,304 ERROR [stderr] (main) at org.wildfly.swarm.bootstrap.Main.main(Main.java:35)
2017-01-08 20:31:01,305 ERROR [stderr] (main) Caused by: org.wildfly.swarm.container.DeploymentException: WFSWARM0007: Deployment failed: {"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"hatts.war\".WeldStartService" => "org.jboss.msc.service.StartException in service jboss.deployment.unit.\"hatts.war\".WeldStartService: Failed to start service
2017-01-08 20:31:01,305 ERROR [stderr] (main) Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type Authenticator with qualifiers @Default
2017-01-08 20:31:01,305 ERROR [stderr] (main) at injection point [UnbackedAnnotatedField] @Inject private com.uss.hatts.rest.acktest.HattsACKTestService.authenticator
2017-01-08 20:31:01,305 ERROR [stderr] (main) at com.uss.hatts.rest.acktest.HattsACKTestService.authenticator(HattsACKTestService.java:0)
2017-01-08 20:31:01,305 ERROR [stderr] (main) "},"WFLYCTL0412: Required services that are not installed:" => ["jboss.deployment.unit.\"hatts.war\".WeldStartService"],"WFLYCTL0180: Services with missing/unavailable dependencies" => undefined}
2017-01-08 20:31:01,305 ERROR [stderr] (main) at org.wildfly.swarm.container.runtime.RuntimeDeployer.deploy(RuntimeDeployer.java:280)
2017-01-08 20:31:01,306 ERROR [stderr] (main) at org.wildfly.swarm.container.runtime.RuntimeDeployer.deploy(RuntimeDeployer.java:95)
2017-01-08 20:31:01,306 ERROR [stderr] (main) at org.wildfly.swarm.Swarm.deploy(Swarm.java:475)
2017-01-08 20:31:01,306 ERROR [stderr] (main) at org.wildfly.swarm.Swarm.main(Swarm.java:612)
2017-01-08 20:31:01,308 ERROR [stderr] (main) ... 7 more
答案 0 :(得分:2)
您的Maven依赖项必须是:
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>jaxrs</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>cdi</artifactId>
</dependency>
在几个版本之前, weld
已重命名为cdi
,并且不再明确需要jaxrs-cdi
。它为你带来了。
根据您在自定义main()中所拥有的内容,我建议您不要使用它,只需让WF Swarm为您创建部署。