我想在使用REST调用时通过我的dao访问数据。我的dao适用于webapp。但是当我使用我创建的web服务时,我有一个空指针异常。
这是我的代码:
@Path("/hello")
public class HelloWorldResource {
@Inject
private IAppDao appDao;
@GET
@Produces(MediaType.APPLICATION_XML)
@Path("/{idApp}/{currentVersion}")
public Object checkUpdate(@PathParam("idApp") String idApp,
@PathParam("currentVersion") String currentVersion) {
if(idApp == null || idApp.isEmpty() || currentVersion == null || currentVersion.isEmpty())
{
ErrorResponse error = new ErrorResponse();
error.setCode(400);
error.setMessage("bad_request_error");
error.setReturnUrl(false);
return Response.status(400).entity(error).build();
}
App app = appDao.findAppByName(idApp);
if(app != null)
{
AppResponse response = new AppResponse();
response.setDataVersion(app.getDataVersion());
response.setName(app.getName());
response.setUpdatable(app.isUpdatable());
response.setVersion(app.getVersion());
return Response.status(200).entity(response).build();
}
else
{
ErrorResponse error = new ErrorResponse();
error.setCode(400);
error.setMessage("bad_app_name_error");
error.setReturnUrl(false);
return Response.status(400).entity(error).build();
}
}
和错误堆栈:
12 mars 2014 15:42:33 com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException
GRAVE: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
java.lang.NullPointerException
at com.mlwebanddev.website.front.jersey.HelloWorldResource.checkUpdate(HelloWorldResource.java:38)
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$ObjectOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:258)
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:302)
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:1542)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1473)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:540)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:715)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:113)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tapestry5.TapestryFilter.doFilter(TapestryFilter.java:175)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.mlwebanddev.website.tools.Log4jFilter.doFilter(Log4jFilter.java:49)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:695)
空指针在该行上:
App app = appDao.findAppByName(idApp);
有什么想法解决这个问题吗?
由于
答案 0 :(得分:0)
applicationContext.xml中的配置是:
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="resthub" />
<property name="persistenceUnitManager" ref="scanningPersistenceUnitManager" />
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="false" />
</bean>
</property>
<property name="jpaProperties">
<map>
<entry key="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<!-- Utile juste pour créer les tables EN LOCAL, ne pas commiter décommenté -->
<entry key="hibernate.hbm2ddl.auto" value="update" />
</map>
</property>
</bean>
答案 1 :(得分:0)
我的@Stateless EJB遇到了同样的问题。
我决定将@Stateless添加到我公共类的声明之上。像这样:
@Path("/hello")
@Stateless
public class HelloWorldResource {
@EJB
private DaoHello daoHello;
(...)