org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是 java.lang.NullPointerException in REST with Spring
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34354481/
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
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException in REST with Spring
提问by Rishi
I am trying to create REST service using Spring.When I try to access that service it returns proper data but shows -
我正在尝试使用 Spring 创建 REST 服务。当我尝试访问该服务时,它返回正确的数据但显示 -
Exception handling request to /RESTServ/user/2: org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982) [spring-webmvc-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) [spring-webmvc-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:687) [jboss-servlet-api_3.1_spec-1.0.0.Final.jar:1.0.0.Final]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) [spring-webmvc-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) [jboss-servlet-api_3.1_spec-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:61) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:113) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:56) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:45) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:61) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:70) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:240) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:227) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.servlet.handlers.ServletInitialHandler.accesspublic class User {
private int userid;
private String firstName;
private String lastName;
private String email;
public int getUserid() {
return userid;
}
public void setUserid(int userid) {
this.userid = userid;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User [userid=" + userid + ", firstName=" + firstName
+ ", lastName=" + lastName + ", email=" + email + "]";
}
}
0(ServletInitialHandler.java:73) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.servlet.handlers.ServletInitialHandler.handleRequest(ServletInitialHandler.java:146) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.server.Connectors.executeRootHandler(Connectors.java:177) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.server.HttpServerExchange.run(HttpServerExchange.java:727) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [rt.jar:1.8.0_45]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [rt.jar:1.8.0_45]
at java.lang.Thread.run(Thread.java:745) [rt.jar:1.8.0_45]
Caused by: java.lang.NullPointerException
at java.util.ArrayList.<init>(ArrayList.java:177) [rt.jar:1.8.0_45]
at io.undertow.servlet.spec.HttpServletResponseImpl.getHeaders(HttpServletResponseImpl.java:248) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
at org.springframework.http.server.ServletServerHttpResponse$ServletResponseHttpHeaders.get(ServletServerHttpResponse.java:160) [spring-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.http.server.ServletServerHttpResponse$ServletResponseHttpHeaders.containsKey(ServletServerHttpResponse.java:142) [spring-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.addContentDispositionHeader(AbstractMessageConverterMethodProcessor.java:349) [spring-webmvc-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:221) [spring-webmvc-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:153) [spring-webmvc-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:165) [spring-webmvc-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:80) [spring-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:126) [spring-webmvc-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:814) [spring-webmvc-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:737) [spring-webmvc-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) [spring-webmvc-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959) [spring-webmvc-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893) [spring-webmvc-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) [spring-webmvc-4.2.3.RELEASE.jar:4.2.3.RELEASE]
... 30 more
Can anyone help me with this problem? I have checked for the data it returns and it is not null by applying breakpoints but still it shows above exception.
谁能帮我解决这个问题?我已经检查了它返回的数据,通过应用断点它不为空,但它仍然显示上述异常。
EDITED:
编辑:
Code is: User.javapackage com.rest.service;
代码为: User.java包com.rest.service;
package com.rest.controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.rest.dao.UserService;
import com.rest.service.User;
@RestController
@RequestMapping("/user/")
public class SpringServiceController {
UserService userService = new UserService();
@RequestMapping(value = "/{id}", method = RequestMethod.GET, headers = "Accept=application/json")
public User getUser(@PathVariable int id) {
User user = userService.getUserById(id);
return user;
}
}
SpringServiceController.java
SpringServiceController.java
package com.rest.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.rest.service.User;
import com.rest.utility.DBUtility;
public class UserService {
private Connection connection;
public UserService() {
connection = DBUtility.getConnection();
}
public List<User> getAllUsers() {
List<User> users = new ArrayList<User>();
try {
Statement statement = connection.createStatement();
ResultSet rs = statement
.executeQuery("select * from tblUser");
while (rs.next()) {
User user = new User();
user.setUserid(rs.getInt("userid"));
user.setFirstName(rs.getString("firstname"));
user.setLastName(rs.getString("lastname"));
user.setEmail(rs.getString("email"));
users.add(user);
}
} catch (SQLException e) {
e.printStackTrace();
}
return users;
}
public User getUserById(int userId) {
User user = new User();
try {
PreparedStatement preparedStatement = connection.prepareStatement("select * from tblUser where userid=?");
preparedStatement.setInt(1, userId);
ResultSet rs = preparedStatement.executeQuery();
if (rs.next()) {
user.setUserid(rs.getInt("userid"));
user.setFirstName(rs.getString("firstname"));
user.setLastName(rs.getString("lastname"));
user.setEmail(rs.getString("email"));
}
} catch (SQLException e) {
e.printStackTrace();
}catch (Exception e) {
System.out.println("Exception is: "+e);
e.printStackTrace();
}
return user;
}
}
I am getting data from database using UserService.java DBUtilityis class to connect to JDBC
我正在使用 UserService.java 从数据库获取数据 DBUtility是连接到 JDBC 的类
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>RESTServ</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>rest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/rest-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>rest</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
My web.xml is
我的 web.xml 是
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
<context:component-scan base-package="com.rest.controller" />
<mvc:annotation-driven />
</beans>
And this is my rest-servlet.xml
这是我的 rest-servlet.xml
@Override
public String getHeaders(final String name) {
return new ArrayList<String>(exchange.getResponseHeaders().get(name));
}
PS: One more thing when I delete annotation-driven tag from spring xml file then it works for simple datatype. But it doesn't work where json conversion is required.I am using WildFly - 8.1
PS:当我从 spring xml 文件中删除注释驱动的标签时还有一件事,它适用于简单的数据类型。但它在需要 json 转换的地方不起作用。我正在使用 WildFly - 8.1
回答by Nikhi K. Bansal
this is an issue with WildFly - 8.1 you have to use WildFly - 8.2 or use Jboss 7.
这是 WildFly - 8.1 的问题,您必须使用 WildFly - 8.2 或使用 Jboss 7。
回答by Ingvar Evaldsson
We faced this issue while trial to migrate spring-boot-1.2.2-Final on wildfly 8.1.0-Final to spring-boot-1.3.2-Final on wildfly 8.1.0-Final. This error does not occur on wildfly 8.2.1-Final, so if you have option tp udate your wildfly version, you should do that.
我们在尝试将 wildfly 8.1.0-Final 上的 spring-boot-1.2.2-Final 迁移到 wildfly 8.1.0-Final 上的 spring-boot-1.3.2-Final 时遇到了这个问题。这个错误不会发生在 wildfly 8.2.1-Final 上,所以如果你有选项 tp udate 你的 wildfly 版本,你应该这样做。
But if you still have to use wildfly-8.1.0-Final, then you could patchit.
但是如果你仍然必须使用wildfly-8.1.0-Final,那么你可以修补它。
1) get https://github.com/undertow-io/undertow/archive/1.0.15.Final.zip
1) 获取https://github.com/undertow-io/undertow/archive/1.0.15.Final.zip
2) edit io.undertow.servlet.spec.HttpServletResponseImpl located in module servlet
2) 编辑位于模块 servlet 中的 io.undertow.servlet.spec.HttpServletResponseImpl
@Override
public Collection<String> getHeaders(final String name) {
final HeaderValues headerValues = exchange.getResponseHeaders().get(name);
if (headerValues != null) {
return new ArrayList<String>(headerValues);
}
return new ArrayList<String>();
}
to
到
import io.undertow.util.HeaderValues;
Add the HeaderValues
import:
添加HeaderValues
导入:
<dependency>
<groupId>org.codehaus.Hymanson</groupId>
<artifactId>Hymanson-jaxrs</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.codehaus.Hymanson</groupId>
<artifactId>Hymanson-xc</artifactId>
<version>1.9.13</version>
</dependency>
package undertow-servlet with maven and then overwrite
用 maven 打包 undertow-servlet 然后覆盖
{wildfly-root-directory}/modules/system/layers/base/io/undertow/servlet/main/undertow-servlet-1.0.15.Final.jar
{wildfly-root-directory}/modules/system/layers/base/io/undertow/servlet/main/undertow-servlet-1.0.15.Final.jar
with the file
与文件
{undertow-root-directory}/servlet/target/undertow-servlet-1.0.15.Final.jar
{undertow-root-directory}/servlet/target/undertow-servlet-1.0.15.Final.jar
This will solve this error.
这将解决此错误。
回答by Omkar
If your are using maven
, add JSON Databinding
dependencies specified below in your pom.xml
and annotate the User
class with @JsonRootName(value = "user")
,
如果您正在使用maven
,请JSON Databinding
在您的下面添加依赖项,pom.xml
并使用 注释User
该类@JsonRootName(value = "user")
,
回答by Lev
This may be helpful to those who aren't able to upgrade past Wildfly 8.1.0 in their project to address this problem. Ingvar's answer above of patching the code helped me out a lot, and here is a script that will perform the patch in one command for you (keeping a backup of your previous copy):
这可能对那些无法在项目中升级到 Wildfly 8.1.0 以解决此问题的人有所帮助。上面 Ingvar 对代码打补丁的回答对我帮助很大,这里有一个脚本可以在一个命令中为你执行补丁(保留你以前的副本的备份):