我正在使用带有 Jackson 处理器的 Spring MVC。当 JSON 请求作为 POST 请求发送到服务器时,@RequestBody 被反序列化为我需要的对象。问题出现在发送 GET 请求时,它实际上显示 Http 500 Internal Server 错误。抛出的异常是:
java.io.EOFException: No content to map to Object due to end of input
org.codehaus.jackson.map.ObjectMapper._initForReading(ObjectMapper.java:2022)
org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:1974)
org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1331)
org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readInternal (MappingJacksonHttpMessageConverter.java:135)
org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:154)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.readWithMessageConverters(HandlerMethodInvoker.java:633)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveRequestBody(HandlerMethodInvoker.java:597)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments (HandlerMethodInvoker.java:346)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
不发送空字符串,并且将正确的 JSON 发送到服务器。我不知道为什么会发生这种情况。下面是我的代码:
JSP-index.jsp
<%@page language="java" contentType="text/html"%>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$('#myForm').submit(function() {
var form = $( this ),
url = form.attr('action'),
userId = form.find('input[name="userId"]').val(),
dat = JSON.stringify({ "userId" : userId });
$.ajax({
url : url,
type : "GET",
traditional : true,
contentType : "application/json",
dataType : "json",
data : dat,
success : function (response) {
alert('success ' + response);
},
error : function (response) {
alert('error ' + response);
},
});
return false;
});
});
</script>
</head>
<body>
<h2>Application</h2>
<form id="myForm" action="/application/user/find" method="GET">
<input type="text" name="userId" value="user1">
<input type="submit" value="Submit">
</form>
</body>
</html>
一旦我在 JSP 和 Controller 方法中将请求类型更改为 POST,一切似乎都会正常工作。
package com.web;
@Controller
@RequestMapping("/user/*")
public class UserController {
@RequestMapping(value = "find", method = RequestMethod.GET, headers = {"content-type=application/json"})
public @ResponseBody UserResponse save(@RequestBody User user) throws Exception {
UserResponse userResponse = new UserResponse();
System.out.println("UserId :" + " " + user.getUserId());
return userResponse;
}
beans.xml
<context:component-scan base-package="com.web"/>
<mvc:annotation-driven/>
<context:annotation-config/>
用户.java
public class User implements Serializable {
private String userId;
public User() {
}
// Getters and setters
}
当发送 GET 请求时,浏览器通常会显示 %%user%%。这只是一个例子。 Jackson 处理器是否仍会读取 GET 请求?
不知道问题出在哪里。希望您能帮忙。
最佳答案
我在 http://api.jquery.com/jQuery.ajax/ 阅读了以下关于 jQuey.ajax() 中 data 参数的作用的描述。 :
要发送到服务器的数据。如果还不是字符串,它将转换为查询字符串。它附加到 GET 请求的 url 中。 ....
因此它被附加到 URL 中并且没有请求正文。这是与 GET 语义一致的行为,如果您想使用 POST 发布数据。
关于java.io.EOFException Jackson 处理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12531822/