Ho un servizio di pianificazione che ottiene il suo lavoro tramite REST come JSON.WildFly Swarm Nullpointer @Inject bean
La classe 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);
}
}
Il 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");
}
}
Il 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();
}
}
}
E l'ist mySchedule solo un POJO
Il mio problema è che quando I POST
JSON viene ricevuto il NullPointerException
per l'iniettato SchedulerController
.
Ho anche un beans.xml in src/main/webapp/WEB-INF
e nel mio pom.xml
ho il seguente dipendenza per CDI (di saldatura)
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>weld</artifactId>
</dependency>
e
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>1.2</version>
</dependency>
Sto usando sciame wildfly 1.0.0 beta 6
Cosa mi è mancato?
Ho trovato il mio errore. Ho appena dovuto rendere la classe ScheduleRESTResource un ManagedBean. –
E 'stato un altro errore. Ho dovuto usare "jaxrs-weld" invece di "weld" dipendenza. –