RuntimeException无法映射到响应,重新抛出到HTTP容器java.lang.NullPointerException

时间:2013-08-20 19:35:24

标签: spring rest nullpointerexception jersey runtimeexception

我正在尝试使用Spring Framework,RESTful Web Services和Jersey实现从我的数据库验证用户记录。

我使用的是MySQL v5.6.0,Eclipse Galileo,Apache tomcat v6.0

UserAccessWS.java

@Path("/user")
@Service
public class UserAccessWS {
@Autowired
private IUserService userService;
private static Logger LOGGER = Logger.getLogger(UserAccessWS.class);

@POST
@Path("/validateUser")
@Produces({ MediaType.APPLICATION_JSON })
public String getValidateUser(@Context HttpServletRequest request,
        @FormParam("userName") String userName,
        @FormParam("password") String password) throws JSONException {
    LOGGER.info("getValidateUser method");
    Users users = new Users();
    users.setUserName(userName);
    users.setPassword(password);
    List<Users> userList = new ArrayList<Users>();
    userList = userService.validateUser(users);

    JSONObject userInfo = new JSONObject();
    userInfo.put("authorize", true);
    return userInfo.toString();
}

@POST
@Path("/login")
@Produces({ MediaType.APPLICATION_JSON })
public String userLogin(
        @FormParam("userName") String userName,
        @FormParam("password") String password) 
                throws Exception 
{
    LOGGER.info("in UserAccessWS::userLogin()");

    JSONObject json = new JSONObject();
    JSONArray jsonArr = new JSONArray();
    List<Users> userList = new ArrayList<Users>();
    userList = userService.userLogin(userName, password);

此行中指出了空错误 “userList = userService.userLogin(userName,password);”

    //System.out.println(userList);

    if (userList.size() == 0)
    {
        json.put("status", "fail");
    } 
    else 
    {
        json.put("status", "success");
        jsonArr.add(userList.get(0));
        json.put("data", jsonArr);
    }
     //System.out.println(jsonArr.get(0).userId);
    return json.toString();
}

IUserService.java

package com.cisco.service.view;
import java.util.List;
import org.json.JSONObject;
import com.cisco.connectivity.rs.mapper.ForgotPassword;
import com.cisco.connectivity.rs.mapper.PasswordChange;
import com.cisco.connectivity.rs.mapper.Registration;
import com.cisco.model.xml.domain.Email;
import com.cisco.model.xml.domain.Users;
public interface IUserService {

public List<Users> validateUser(Users users);

public List<Users> userLogin(String userName, String password);

public String userChangePassword(PasswordChange change);

public List<Email> userForgotPassword(ForgotPassword forgot);

public JSONObject saveData( Registration registration);

public JSONObject check(String userName);

public JSONObject editData( Registration registration);

public JSONObject deleteData( Registration registration);

public JSONObject getData( Registration registration);

public JSONObject getRegistrationFormData(String userName);
}

的applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/tx/spring-aop-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd"
default-autowire="byName">

<bean id="applicationProperties"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="ignoreResourceNotFound" value="true" />
    <property name="ignoreUnresolvablePlaceholders" value="true" />
    <property name="searchSystemEnvironment" value="true" />
</bean>

<context:component-scan base-package="com.cisco" />

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/pooler_mgmt" />
    <property name="username" value="root" />
    <property name="password" value="" />
</bean>

<bean id="systemProperties" class="java.util.HashMap"></bean>
<bean id="systemEnvironment" class="java.util.HashMap"></bean>

的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_2_5.xsd"
id="WebApp_ID" version="2.5">

<display-name>CiscoPoolerMgmt</display-name>
<description>CiscoPoolerMgmt</description>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:/CiscoPoolerMgmt/config/applicationContext.xml</param-value>
</context-param>

<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath*:/CiscoPoolerMgmt/config/log4j.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


<!-- REST web service -->
<servlet>
    <servlet-name>REST_stat_web_service</servlet-name>
    <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.resourceConfigClass</param-name>
        <param-value>com.sun.jersey.api.core.PackagesResourceConfig</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.cisco.ws</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>REST_stat_web_service</servlet-name>
    <url-pattern>/cisco_BI/rest/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>

我得到的错误是:

SEVERE: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
java.lang.NullPointerException
at com.cisco.ws.UserAccessWS.userLogin(UserAccessWS.java:69)
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.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:149)
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:67)
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:259)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:133)
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:83)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:133)
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:71)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:990)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:941)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:932)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:384)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:451)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:632)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
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:102)
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:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)
Aug 21, 2013 12:54:21 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet REST_stat_web_service threw exception
java.lang.NullPointerException
at com.cisco.ws.UserAccessWS.userLogin(UserAccessWS.java:69)
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.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:149)
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:67)
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:259)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:133)
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:83)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:133)
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:71)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:990)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:941)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:932)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:384)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:451)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:632)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
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:102)
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:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)

用于验证用户输入的详细信息的数据库不为空。它有记录。但是通过这个查询,它总是给出一个NullPointerException。

请帮帮我。

1 个答案:

答案 0 :(得分:0)

可能这是迟到的回复。我怀疑userService它的自我是空的。它没有正确自动装配。因为您在default-autowire="byName"中提到了applicationContext.xml。我无法在userService中找到名为applicationContext.xml的bean。我认为您需要在applicationContext.xml

中添加以下行
 <bean id="userService" class="yourIUserServiceImplClass"></bean>