6

我有一个计划服务,它通过 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

我的问题是,当我使用POSTJSON 格式时,我得到了一个NullPointerException用于注入的SchedulerController.

我也有一个 beans.xml,src/main/webapp/WEB-INF在我pom.xml的 CDI (weld) 中有以下依赖项

<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

我错过了什么?

4

1 回答 1

0

我认为您缺少 Swarm CDI 部分。尝试将其添加到您的依赖项中。

<dependency>
  <groupId>org.wildfly.swarm</groupId>
  <artifactId>cdi</artifactId>
</dependency>
于 2017-09-24T21:51:17.433 回答