1

我正在使用 JAX-RS 创建一个休息网络服务。当我将后端类作为 java 应用程序运行时,它会正常运行。但是当我在服务器上运行它并尝试访问如下资源时:

http://localhost:8080/WS/rest/metric/10  

我得到以下异常:

ERROR [http-bio-8080-exec-3] (Database.java:42) - Exception occured: No suitable driver found for jdbc:mysql://localhost:3306/
ERROR [http-bio-8080-exec-3] (Database.java:42) - Exception occured: No suitable driver  found for jdbc:mysql://localhost:3306/
Jun 19, 2012 11:27:29 AM com.sun.jersey.spi.container.ContainerResponse  mapMappableContainerException
SEVERE: The RuntimeException could not be mapped to a response, re-throwing to the HTTP  container
java.lang.NullPointerException
.....

前两行是我的数据库连接方法中的日志:

public static Connection connect()
{
    try
    {
        return DriverManager.getConnection(domain, username, password);
    }

    catch (Exception e)
    {
        logger.error("Exception occured: "+ e.getMessage());
        return null;
    }
}

找不到驱动程序导致 NullPointerException。我已将驱动程序 jar 文件复制到 WEB-INF/lib。但问题仍然是一样的。

这是完整的异常跟踪:

 Jun 19, 2012 11:27:29 AM com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException
SEVERE: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
java.lang.NullPointerException
at com.hp.silas.dao.EstimationDAOImpl.getLatestResult(EstimationDAOImpl.java:126)
at com.hp.silas.ws.EstimationResource.getLastEstimation(EstimationResource.java:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
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.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:1483)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1414)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1363)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1353)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:414)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:708)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
4

2 回答 2

2

缺少的部分是使用此行注册驱动程序:

Class.forName("com.mysql.jdbc.Driver"); 

所以连接方法看起来像:

public static Connection connect()
{
    try
    {
        Class.forName("com.mysql.jdbc.Driver");   
        return DriverManager.getConnection(domain, username, password);
    }

    catch (Exception e)
    {
        logger.error("Exception occured: "+ e.getMessage());
        return null;
    }
}
于 2012-06-19T12:22:51.430 回答
0

注册司机?

DriverManager.registerDriver("...");
于 2012-06-19T10:47:50.367 回答