java.io.EOFException:由于输入结束Spring MVC Jackson Processor,没有要映射到Object的内容

时间:2012-09-20 15:23:14

标签: java json jsp spring-mvc

我使用Spring MVC和JSP将JSON发送到Spring MVC Controller。实际上,我的JSON适用于1种方法,但不适用于另一种方法,我不明白为什么。代码如下:

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 : "POST", 
        traditional : true, 
        contentType : "application/json", 
        dataType : "json", 
        data : dat, 
        success : function (response) { 
            alert('success ' + response); 
        }, 
        error : function (response) { 
            alert('error ' + response); 
        }, 
    }); 

    return false; 
   }); 
 }); 
</script>

<script type="text/javascript">
$(function() {  
$('#emailForm').submit(function() { 
    var form = $( this ), 
        url = form.attr('action'), 
        from = form.find('input[name="from"]').val(),
        to = form.find('input[name="to"]').val(),
        body = form.find('input[name="body"]').val(),
        subject = form.find('input[name="subject"]').val(),
        fileName = form.find('input[name="fileName"]').val(),
        location = form.find('input[name="location"]').val(),
        dat = JSON.stringify({ "from" : from,"to" : to,"body" : body,"subject" : subject,"fileName" : fileName,"location" : location }); 

    $.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/history/save" method="POST">
    <input type="text" name="userId" value="JUnit">
    <input type="submit" value="Submit">
</form>
<form id="emailForm" action="/application/emailService/sendEmail" method="GET">
    <input type="text" name="from" value="name@localhost.com">
    <input type="text" name="to" value="user@localhost.com">
    <input type="text" name="body" value="JUnit E-mail">
    <input type="text" name="subject" value="Email">
    <input type="text" name="fileName" value="attachment">
    <input type="text" name="location" value="location">
    <input type="submit" value="Send Email">
</form>
</body>
</html>

第一种形式正常工作,并在Spring MVC中保存。该代码的示例是:

@Controller
@RequestMapping("/history/*")
public class HistoryController {

@RequestMapping(value = "save", method = RequestMethod.POST, headers = {"content-type=application/json"})
public @ResponseBody UserResponse save(@RequestBody User user) throws Exception {
    UserResponse userResponse = new UserResponse();
    return userResponse;
}

对于第二种形式,我得到例外:

@Controller
@RequestMapping("/emailService/*")
public class EmailController {

@RequestMapping(value = "sendEmail", method = RequestMethod.GET, headers = {"content-type=application/json"})
public void sendEmail(@RequestBody Email email) {
    System.out.println("Email Body:" + " " + email.getBody());
    System.out.println("Email To: " + " " + email.getTo());
}
}

下面是堆栈跟踪:

 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)

我也试过使用POST,但仍然是同样的问题。我需要使用JSP将JSON数据发送到Spring MVC Controller。

我不确定问题出在哪里。

的beans.xml

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

    <mvc:annotation-driven/>

    <context:annotation-config/>

有什么想法吗?

修改

public class Email implements Serializable {

private String from;
private String to;
private String body;
private String subject;
private String fileName;
private String location;

public Email() {

}

// Getters and setters
}

发送的JSON是form2,即#emailForm。

3 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,但我使用curl来验证服务器端功能。

我的curl命令中的初始数据部分是

-d "{'id':'123456', 'name':'QWERT'}"

将命令更改为

-d '{"id":"123456", "name":"QWERT"}'

然后它奏效了。

希望这会给出一些提示。

答案 1 :(得分:1)

您应该检查是否设置了“内容长度”标题。

答案 2 :(得分:0)

当我尝试使用带有路径参数的POST方法时遇到了这个问题,但是我忘记放'@PathParam("id") Integer id'了,我只是将'Integer id '放在了参数中。