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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18343594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 06:09:58  来源:igfitidea点击:

RuntimeException could not be mapped to a response, re-throwing to the HTTP container java.lang.NullPointerException

springrestnullpointerexceptionjerseyruntimeexception

提问by user2639869

I am trying to validate a user record from my database using Spring Framework, RESTful Web Services and Jersey Implementation.

我正在尝试使用 Spring 框架、RESTful Web 服务和 Jersey 实现从我的数据库中验证用户记录。

I am using MySQL v5.6.0, Eclipse Galileo, Apache tomcat v6.0

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

UserAccessWS.java

用户访问WS.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);

Null error is being pointed out in this line "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

用户服务.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

<?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

<?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>

Error which I am get is:

我得到的错误是:

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)

The database from where it has to validate the details entered by user is NOT empty. It has the records in them. But via this query it always gives a NullPointerException.

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

Please help me out.

请帮帮我。

回答by Prabhakar Manthena

May be this is a late reply. I am suspecting the userServiceit self is null. It is not autowired properly. Because here you mentioned default-autowire="byName"in your applicationContext.xml. I couldn't find the bean named userServicein your applicationContext.xml. I think you need to add the below line in you applicationContext.xml

可能这是一个迟到的回复。我怀疑userService它自己是空的。它没有正确自动装配。因为这里你default-autowire="byName"在你的applicationContext.xml. 我userService在你的applicationContext.xml. 我认为您需要在您中添加以下行applicationContext.xml

 <bean id="userService" class="yourIUserServiceImplClass"></bean>