I am trying to implement a demo web service but I struggle with CDI. I want to inject FooFinder
into FooBooker
but I am getting null
instead (there is a NullPointerException during the call of FooResponse as fooFinder is null).
I have an interface
@RequestScoped
@Named
public interface FooFinder {
FooResponse getReply(String origin, String destination);
}
implemented like
@RequestScoped
@Named
public class MockedFoo implements FooFinder {
@Override
public FooResponse getReply(String origin, String destination) {
String s = "SuperFoo";
Double d = 123.45;
return new FooResponse(origin, destination, s, d);
}
}
within a web service
@WebService
public class FooBooker {
@Inject
FooFinder fooFinder;
@WebMethod
public FooResponse getReply(String origin, String destination) {
return fooFinder.getReply(origin, destination);
}
public static void main(String[] argv) {
Object implementor = new FooBooker();
String address = "http://localhost:9000/FooBooker";
Endpoint.publish(address, implementor);
}
}
It turned out that there was a problem with CDI libraries setup. I am using IntelliJ IDEA and I have enabled CDI support in GlassFish configuration dialogue.
Injection however didn’t work until I added CDI support in Modules configuration.