【问题标题】:java.io.EOFException: No content to map to Object due to end of input Spring MVC Jackson Processorjava.io.EOFException:由于输入 Spring MVC Jackson 处理器结束,没有要映射到对象的内容
【发布时间】:2012-09-13 00:11:55
【问题描述】:

我正在使用 Spring MVC 和 JSP 将 JSON 发送到 Spring MVC 控制器。实际上,我的 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 : "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 控制器。

我不确定问题出在哪里。

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。

【问题讨论】:

  • 你的Email 类是什么样子的?发送到该控制器的 JSON 是什么?请编辑您的问题并添加这两个详细信息。
  • 请参阅上面的更改。 JSON 可以在 emailForm 表单标签中看到。
  • 只是让你知道,你不需要字符串化你的对象文字。 jQuery 将接受一个对象文字就好了。
  • 您确定数据正在到达控制器吗?看看你的网络标签。如果您发送的是空的 POST 或 GET,则可能会发生此异常。
  • 是的,我确信数据正在到达控制器的应用程序/保存方法。我尝试对 emailService 使用 POST 并且数据到达服务器。但是,问题仅在于 GET。有什么想法吗?

标签: java json jsp spring-mvc


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

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

    在我把命令改成

    之后

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

    然后它起作用了。

    希望这能提供一些提示。

    【讨论】:

    • 我的原因是方法中请求正文参数后的额外参数。
    【解决方案3】:

    你应该检查你是否设置了“content-length”标题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-23
      • 2019-04-13
      • 2015-01-11
      相关资源
      最近更新 更多