0

我有一个在 JBOSS AS 7 上运行的 RESTeasy 服务,它试图创建与本地数据库(mysql 服务器)的连接。

数据源在 JBOSS 管理中正确定义并且连接良好(JSF 应用程序可以很好地连接它并且它可以修改数据库),但是当我尝试在我的 RESTeasy 服务中连接时,它给了我一个 nullPointerException 我可以' t似乎弄清楚为什么。kumonobs 是有问题的数据库。错误发生在通过post请求调用的persist方法中。任何帮助将不胜感激,如果需要任何其他信息,请询问!

@Path("/RSAndroid")
@ApplicationScoped
public class HelloWorldResource implements Serializable{
@Resource(mappedName = "java:jboss/datasources/kumonobs")
private static DataSource dataSource;

private static ArrayList<Student> students = new ArrayList<Student>();


@GET()
@Produces("text/plain")
public String sayHello() {
    return stringStudents(students);
}

@POST()
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public String postStudent(@FormParam("Student ID") String student){
    Date date = new Date();
    Student kumonstudent = new Student(student, date);
    studentCheck(kumonstudent); 
    System.out.println(kumonstudent.toString());
    return "OK";
}


public String stringStudents(ArrayList<Student> s){
    String students = "";
    for(Student student : s){
        students += student.toString();
    }   
    return students;
}

public void studentCheck(Student student){
    boolean checkIn = true;
    int count = 0;

    for(Student s : students){
        count++;
        //If Student has been found in the list.
        if(s.getStudentID().equals(student.getStudentID())){
            //remove the student from the list.
            students.remove(count-1);
            // ** DO DATABASE STUFF HERE FOR REMOVING STUDENT **
            checkIn = false;
            break;
        }
    }
    //If student needs to be checked in, add to list.
    if(checkIn){
        System.out.println(student.getDate());
        students.add(student);
        // ** DO DATABASE STUFF HERE FOR ADDING STUDENT **
        persist(student);


    }
}
//Insert student into database table.
public void persist(Student student) {
     PreparedStatement stmt = null;
       Connection connection = null;
       try {
           try {
               connection = dataSource.getConnection(); //ERROR OCCURS HERE NULLPOINTER
            try {
                 System.out.println("Fourth");
                stmt = connection.prepareStatement(
                        "INSERT INTO checkin VALUES (?, ?)");
                stmt.setString(1, "A00105010");
                stmt.setString(2, "Sample Name");
                stmt.executeUpdate();
            } finally {
                if (stmt != null) {
                    stmt.close();
                }
            }
        } finally {
            if (connection != null) {
                connection.close();
            }
        }
    } catch (SQLException ex) {
        System.out.println("Error in persist ");
        ex.printStackTrace();
    }
}
}

14:29:22,040 INFO  [stdout] (http--0.0.0.0-8080-1) 2012-11-06
14:29:22,040 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].    [/AndroidRS].[Resteasy]] (http--0.0.0.0-8080-1) Servlet.service() for servlet R
esteasy threw exception: org.jboss.resteasy.spi.UnhandledException:    java.lang.NullPointerException
        at         org.jboss.resteasy.core.SynchronousDispatcher.handleApplicationException(SynchronousDispatcher.java:340) [resteasy-jaxrs-2.3.2.Final.jar:]
        at     org.jboss.resteasy.core.SynchronousDispatcher.handleException(SynchronousDispatcher.java:214)     [resteasy-jaxrs-2.3.2.Final.jar:]
        at org.jboss.resteasy.core.SynchronousDispatcher.handleInvokerException(SynchronousDispatcher.java:190) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:540) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:502) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:119) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:208) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:55) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:50) [resteasy-jaxrs-2.3.2.Final.jar:]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
    at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
    at java.lang.Thread.run(Thread.java:662) [rt.jar:1.6.0_27]
Caused by: java.lang.NullPointerException
    at org.jboss.samples.rs.webservices.HelloWorldResource.persist(HelloWorldResource.java:92) [classes:]
    at org.jboss.samples.rs.webservices.HelloWorldResource.studentCheck(HelloWorldResource.java:81) [classes:]
    at org.jboss.samples.rs.webservices.HelloWorldResource.postStudent(HelloWorldResource.java:44) [classes:]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.6.0_27]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [rt.jar:1.6.0_27]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [rt.jar:1.6.0_27]
    at java.lang.reflect.Method.invoke(Method.java:597) [rt.jar:1.6.0_27]
    at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:155) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.core.ResourceMethod.invokeOnTarget(ResourceMethod.java:257) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:222) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:211) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:525) [resteasy-jaxrs-2.3.2.Final.jar:]
    ... 19 more
4

1 回答 1

0

找到了解决方案,而不是使用数据源的注释:

String strDSName = "java:jboss/datasources/thenamehere";
ctx = new InitialContext();
dataSource = (javax.sql.DataSource) ctx.lookup(strDSName);
于 2012-11-07T17:46:23.583 回答