Reputation: 351
First I define a jersey resource, it defines a class GetAllProgramDesc, then inject a java beans "IDataDevProgram" into it.
@Path("/datadev")
public class GetAllProgramDesc {
private IDataDevProgram dataProgram;
public IDataDevProgram getDataProgram() {
return dataProgram;
}
public void setDataProgram(IDataDevProgram dataProgram) {
this.dataProgram = dataProgram;
}
// The Java method will produce content identified by the MIME Media
// type "text/plain"
@GET @Path("/mbpprograms")
@Produces("application/json")
public String getClichedMessage() {
// Return some cliched textual content
List<MbpProgram> list=dataProgram.showMbpProgramList(21294551);
return JSONObject.toJSONString(list);
}
}
Then I wanna to inject a java beans into jersey resource class:
<bean id="dataDevProgram" class="com.taobao.gemstone.data.mbpapi.datadev.DataDevProgram">
<property name="mDBops" ref="dataDevDBOps" />
<property name="sqlManager" ref="sqlManger" />
<property name="actionManager" ref="actionManger" />
<property name="dataManager" ref="dataManageImpl" />
<property name="constant" ref="constantproperty"/>
</bean>
<bean id="datadevrest" class="com.taobao.gemstone.data.mbpapi.restresources.GetAllProgramDesc">
<property name="dataProgram" ref="dataDevProgram" />
</bean>
However, when i send a query to this url, the whole process crashed as follows:
java.lang.NullPointerException com.taobao.gemstone.data.mbpapi.restresources.GetAllProgramDesc.getClichedMessage(GetAllProgramDesc.java:48) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke(Method.java:601)
is there any method to deal with this problem?
Upvotes: 1
Views: 464
Reputation: 19020
If you want to use Spring in Jersey you can use one of the following:
Inject @Context ServletContext context;
into your resource class,
then use code such as:
WebApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
IDataDevProgram dataProgram = applicationContext.getBean(IDataDevProgram .class);
Alternatively, you can also use jersey support for IoC container:
@InjectParam IDataDevProgram dataProgram
You will have to use jersey-spring
contrib and apply SpringServlet
in your web.xml
, there are numerous short tutorials which explain how to do that
Upvotes: 2