I am using Grizzly as a web server to create standalone REST service with Jersey. I also want to use Spring IoC in the application but I ran into problems with auto-wiring beans.
To create the server I run this in my main() method:
HttpServer server = null;
try {
ResourceConfig rc = new PackagesResourceConfig("");
ConfigurableApplicationContext cac = new ClassPathXmlApplicationContext("classpath*:/spring-context.xml");
IoCComponentProviderFactory factory = new SpringComponentProviderFactory(rc,
server = GrizzlyServerFactory.createHttpServer("http://localhost:5555", rc, factory);
System.in.read();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (server != null) {
server.stop();
}
} catch (Exception e) {
e.printStackTrace();
}
}
My spring-context.xml contains just below:
<context:annotation-config />
<context:component-scan base-package="com.test"/>
I want to auto-wire BO object in my Jersey resource class:
@Path("/test")
@Service
public class Test {
@Autowired
private testBO testBo;
....
@GET
@Path("/info")
public String getInfo() {
return testBo.get();
}
}
Now, whenever I call a method in the Test class that references testBo, I am getting NullPointerException , which I think means the bean was not auto-wired.
testBO class has @Component annotation. Also all relevant beans are part of "com.test" package or its subpackages.
Anyone has any idea what am I doing wrong?
As requested stack trace for NullPointerException below:
java.lang.NullPointerException
at com.test.Test.getInfo(Test.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
at com.sun.jersey.server.impl.container.grizzly2.GrizzlyContainer._service(GrizzlyContainer.java:215)
at com.sun.jersey.server.impl.container.grizzly2.GrizzlyContainer.service(GrizzlyContainer.java:185)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:162)
at org.glassfish.grizzly.http.server.HttpHandlerChain.service(HttpHandlerChain.java:195)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:162)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:160)
at org.glassfish.grizzly.filterchain.ExecutorResolver$3.execute(ExecutorResolver.java:95)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:444)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:364)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:290)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:133)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:76)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:63)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:823)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:116)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$000(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$1.run(WorkerThreadIOStrategy.java:98)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:508)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:488)
at java.lang.Thread.run(Thread.java:662)
I am also seeing that in the console output:
03-Aug-2013 17:46:33 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@131f71a: startup date [Sat Aug 03 17:46:33 BST 2013]; root of context hierarchy
03-Aug-2013 17:46:33 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1971afc: defining beans []; root of factory hierarchy
If you have Java 8 - might be worth taking a look at Microserver .
It provides the infrastructure to integrate Grizzly, Jersey 2 and Spring 4.0. If your main method is in package com.test, then all you need is :-
public static void main(String[] args){
new MicroserverApp(()->"context").run();
}
Otherwise, you should also annotate your main class with
@Microserver(basePackages={"com.test"})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.