WildFly Swarm Nullpointer @Inject bean

时间:2016-03-22 14:00:15

标签: java cdi wildfly weld wildfly-swarm

我有一个计划服务,通过REST作为JSON获得它的工作。

Resource类:

import java.io.IOException;

import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;

import de.fszm.scheduler.controller.SchedulerController;
import de.fszm.scheduler.entities.MySchedule;

@Path("/schedule")
public class ScheduleRESTResource {

    @Inject
    SchedulerController scheduleController;

    @POST
    @Path("/job")
    @Consumes(MediaType.APPLICATION_JSON)
    public void schedule(MySchedule schedule) throws IOException {
        scheduleController.buildSchedule(schedule);
    }
}

ScheduleController

import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

import de.fszm.scheduler.entities.MySchedule;

@Named
@RequestScoped
public class SchedulerController {



    public void buildSchedule(MySchedule schedule) {
        System.out.println("Test");
    }

}

SchedulerMain

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.wildfly.swarm.container.Container;
import org.wildfly.swarm.jaxrs.JAXRSArchive;

import de.fszm.scheduler.controller.SchedulerController;
import de.fszm.scheduler.entities.MySchedule;
import de.fszm.scheduler.rest.JaxRSActivator;
import de.fszm.scheduler.rest.ScheduleRESTResource;

public class SchedulerMain {

    public static void main(String[] args) {

        try {
            Container container = new Container();
            container.start();

            JAXRSArchive appDeployment = ShrinkWrap.create(JAXRSArchive.class);

            appDeployment.addResource(ScheduleRESTResource.class);
            appDeployment.addResource(JaxRSActivator.class);
            appDeployment.addClass(MySchedule.class);
            appDeployment.addClass(SchedulerController.class);
            appDeployment.addAllDependencies();

            container.deploy(appDeployment);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

而MySchedule只是一个POJO

我的问题是,当我POST JSON时,我为注入的NullPointerException获得SchedulerController

我在src/main/webapp/WEB-INFpom.xml中也有一个beans.xml,我对CDI(焊接)有以下依赖

<dependency>
        <groupId>org.wildfly.swarm</groupId>
        <artifactId>weld</artifactId>
</dependency>

<dependency>
        <groupId>javax.enterprise</groupId>
        <artifactId>cdi-api</artifactId>
        <version>1.2</version>
</dependency>

我正在使用WildFly swarm 1.0.0 beta 6

我错过了什么?

1 个答案:

答案 0 :(得分:0)

我认为你错过了Swarm CDI部分。 请尝试将其添加到您的依赖项中。

<dependency>
  <groupId>org.wildfly.swarm</groupId>
  <artifactId>cdi</artifactId>
</dependency>