我正在使用struts2,hibernate,spring和REST使用jersey创建应用程序。我的应用程序的所有部分都在工作,但在我的REST中我得到了空指针
javax.servlet.ServletException: java.lang.NullPointerException
org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:487)
org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:425)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:383)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:336)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:223)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
cause mère
java.lang.NullPointerException
com.project.caritas.rest.MedicamentJsonRest.getMedicament(MedicamentJsonRest.java:35)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:483)
org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:144)
org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:161)
org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:205)
org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99)
org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:326)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
org.glassfish.jersey.internal.Errors.process(Errors.java:315)
org.glassfish.jersey.internal.Errors.process(Errors.java:297)
org.glassfish.jersey.internal.Errors.process(Errors.java:267)
org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)
org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)
org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:471)
org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:425)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:383)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:336)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:223)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
我的春季配置看起来像这样
<bean id="medicamentDAO" class="com.project.caritas.dao.impl.MedicamentDAOImpl" parent="abstractDAO" />
<bean id="medicamentService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" parent="abstractService">
<property name="target">
<bean id="medicamentServiceImpl" class="com.project.caritas.service.impl.MedicamentServiceImpl">
<property name="medicamentDAO" ref="medicamentDAO" />
</bean>
</property>
</bean>
<bean id="medicamentJsonRest" class="com.project.caritas.rest.MedicamentJsonRest">
<property name="medicamentService" ref="medicamentService" />
</bean>
我的REST课程
@Path("/json/medicaments")
public class MedicamentJsonRest {
private MedicamentService medicamentService;
@GET
@Path("/find")
@Produces({MediaType.APPLICATION_JSON})
public Medicaments getMedicament(){
Medicaments m = new Medicaments();
try {
m = medicamentService.find(1);
} catch (NonExistentEntityException ex) {
Logger.getLogger(MedicamentJsonRest.class.getName()).log(Level.SEVERE, null, ex);
}
return m;
}
/**
* @return the medicamentService
*/
public MedicamentService getMedicamentService() {
return medicamentService;
}
/**
* @param medicamentService the medicamentService to set
*/
public void setMedicamentService(MedicamentService medicamentService) {
this.medicamentService = medicamentService;
}
}
Web.xml球衣配置
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.project.caritas.rest.RestApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
我的问题是为什么medicamentService有空值而不是Spring处理? 谢谢你的回答,抱歉我的英文不好
答案 0 :(得分:-1)
我认为问题可能是您将Rest控制器定义为Bean。通常,如果你使用Spring和Jersey,你不会将你的休息控制器定义为bean,因为Jersey会为你启动这些类。只需使用像这样的RestController annoation,这样Autowire和所有基于注释的东西仍然可以工作:
@Path("/json/medicaments")
@RestController
public class MedicamentJsonRest {
...
}
编辑:
同样在servlet中你应该使用:
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>my.package.name</param-value>
</init-param>