Tomcat:Null Pointer尝试访问Web服务时出现异常

时间:2012-12-07 21:26:48

标签: java web-services tomcat web-applications jax-rs

我有一个包含Web服务的动态Web项目。我已将其导出为WAR文件并将其放在tomcat的webapps目录中。 Tomcat显示Web应用程序正在运行。当我尝试调用我的服务的一个操作时,我得到以下异常:

java.lang.NullPointerException
    sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1629)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:461)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:931)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    java.lang.Thread.run(Thread.java:680)

知道这意味着什么吗?

这是我的类定义Web服务:

package webservice;

import java.sql.*;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.core.MediaType;

@Path("/operations")
public class NotifyWebService {

    @Path("/insertNewPatron")
    @GET

    public String insertNewPatron(@QueryParam("cardNumber")String cardNumber,
                                    @QueryParam("pin") String pin,
                                    @QueryParam("nickname") String nickname,
                                    @QueryParam("deviceID")String deviceID) throws Exception {


        String connectionUrl = "jdbc:sqlserver://mssql.acpl.lib.in.us:1433;" +
                   "databaseName=MobileNotify;user=Mobile_Notification_User;password=xxxxxxx;";




            Connection c = DriverManager.getConnection(connectionUrl);

            String insertPatronStatement = "INSERT INTO dbo.Patron VALUES ('"+cardNumber+"','"+pin+"','"+nickname+"')";
            String insertDeviceOwner = "INSERT INTO dbo.DeviceOwner VALUES ('"+deviceID+"','"+cardNumber+"')";
            Statement state = null;

            state = c.createStatement();
            state.executeUpdate(insertPatronStatement);
            state.executeUpdate(insertDeviceOwner);


            c.close();


            return "true";
    }


    @Path("/initializeDevice")
    @GET

    public String initializeDevice( @QueryParam("deviceID")String deviceID) throws Exception{

        String connectionUrl = "jdbc:sqlserver://mssql.acpl.lib.in.us:1433;" +
                   "databaseName=MobileNotify;user=Mobile_Notification_User;password=xxxxxx;";


        Connection c = DriverManager.getConnection(connectionUrl);

        String insertDeviceStatement = "INSERT INTO dbo.Devices VALUES ('"+deviceID+"',1,3,1,3)";
        Statement state = c.createStatement();
        state.executeUpdate(insertDeviceStatement);


        return "true";
    }


    @Path("/updateDevicePreferences")
    @GET

    public String updateDevicePreferences(@QueryParam("deviceID") String deviceID, 
                                            @QueryParam("dueDateNotice")String dueDateNotice,
                                            @QueryParam("dueDateNoticeAdvance") String dueDateNoticeAdvance,
                                            @QueryParam("holdsNotice") String holdsNotice, 
                                            @QueryParam("eventNoticeAdvance")String eventNoticeAdvance) throws Exception{
        String connectionUrl = "jdbc:sqlserver://mssql.acpl.lib.in.us:1433;" +
                   "databaseName=MobileNotify;user=Mobile_Notification_User;password=xxxxxx;";


        Connection c = DriverManager.getConnection(connectionUrl);

        String updateDeviceStatement = "UPDATE dbo.Devices SET dueDateNotice="+dueDateNotice+", dueDateNoticeAdvance="+dueDateNoticeAdvance
                                    +", holdsNotice="+holdsNotice+", eventNoticeAdvance="+eventNoticeAdvance+" WHERE deviceID='"+deviceID+"'";
        Statement state = c.createStatement();
        state.executeUpdate(updateDeviceStatement);

        return "true";
    }


    @Path("/removeUser")
    @GET

    public String removeUser(@QueryParam("cardNumber")String cardNumber) throws Exception{
        String connectionUrl = "jdbc:sqlserver://mssql.acpl.lib.in.us:1433;" +
                   "databaseName=MobileNotify;user=Mobile_Notification_User;password=xxxxxx;";


        Connection c = DriverManager.getConnection(connectionUrl);

        String removeStatement = "DELETE FROM dbo.Patron WHERE cardNumber='"+cardNumber+"'";

        Statement state = c.createStatement();
        state.executeUpdate(removeStatement);




        return "true";
    }

    @Path("/addEvent")
    @GET

    public String addEvent(@QueryParam("deviceID")String deviceID, @QueryParam("eventID")String eventID) throws Exception{

        String connectionUrl = "jdbc:sqlserver://mssql.acpl.lib.in.us:1433;" +
                   "databaseName=MobileNotify;user=Mobile_Notification_User;password=xxxxx;";


        Connection c = DriverManager.getConnection(connectionUrl);

        String eventStatement = "INSERT INTO dbo.Events VALUES ('"+deviceID+"','"+eventID+"')";
        Statement state = c.createStatement();
        state.executeUpdate(eventStatement);


        return "true";
    }

    @Path("/removeEvent")
    @GET

    public String removeEvent(@QueryParam("deviceID")String deviceID, @QueryParam("eventID")String eventID) throws Exception{

        String connectionUrl = "jdbc:sqlserver://mssql.acpl.lib.in.us:1433;" +
                   "databaseName=MobileNotify;user=Mobile_Notification_User;password=xxxxx;";


        Connection c = DriverManager.getConnection(connectionUrl);

        String eventStatement = "DELETE FROM dbo.Events WHERE deviceID='"+deviceID+"' AND eventID='"+eventID+"'";
        Statement state = c.createStatement();
        state.executeUpdate(eventStatement);


        return "true";
    }

    @Path("/removeAllEvents")
    @GET

    public String removeAllEvents(@QueryParam("deviceID")String deviceID) throws Exception{

        String connectionUrl = "jdbc:sqlserver://mssql.acpl.lib.in.us:1433;" +
                   "databaseName=MobileNotify;user=Mobile_Notification_User;password=xxxx;";


        Connection c = DriverManager.getConnection(connectionUrl);

        String eventStatement = "DELETE FROM dbo.Events WHERE deviceID='"+deviceID+"'";
        Statement state = c.createStatement();
        state.executeUpdate(eventStatement);


        return "true";
    }
}

这是我的web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>APNS_WebService</display-name>
   <servlet>
        <servlet-name>javax.ws.rs.core.Application</servlet-name>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>javax.ws.rs.core.Application</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

如果需要更多信息,请与我们联系!

1 个答案:

答案 0 :(得分:2)

我认为NPE的原因是<servlet-class>块中缺少<servlet>元素。但是,决定servlet类应该是什么会引发更大的问题......

看起来您没有选择JAX-RS框架。有关各种备选方案的见解,请参阅JAX-RS Frameworks

我目前正在使用泽西岛。 Jersey的典型web.xml可能包含:

  <servlet>
    <servlet-name>WebService</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
       <param-name>com.sun.jersey.config.property.packages</param-name>
       <param-value>webservice</param-value>
    </init-param>
    <init-param>
      <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
      <param-value>true</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>WebService</servlet-name>
    <url-pattern>/api/rest/*</url-pattern>
  </servlet-mapping>

对于许多JAX-RS框架,框架的选择将确定您应该输入的内容<servlet-class>

另请注意,由于您的根资源位于包webservice中,因此我将com.sun.jersey.config.property.packages参数设置为webservice,因此Jersey将扫描您的包以查找任何JAX-RS注释类。