加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

ObjectMapper throw java.io.EOFException: No content to map to

(2014-08-11 14:43:52)
标签:

json

jackson

eofexception

end

input

分类: 框架

I have a response from a REST web service that looks like this:-

{
   "number":447919191231,
   "messages":"",
   "receipt":{
      "uid":"5d1bddf5-1b98-4a32-8ebf-f71c3973e7a8",
      "messages":"",
      "status":"SUCCESS",
      "code":"09641",
      "ttl":120,
      "url":"http:\/\/server:8080\/x\/d6985"
   }
}

and I am attempting to use the method outlined here in the full data binding example

http://wiki.fasterxml.com/JacksonInFiveMinutes#Full_Data_Binding_.28POJO.29_Example

I have created a class which looks like this:-

public class ReceiptResponse {

    private int _number;
    private String _messages;
    private Receipt _receipt;

    public int getNumber() { return _number; }
    public void setNumber(int number) { this._number = number; }
    public String getMessages() { return _messages; }
    public void setMessages(String messages) { this._messages = messages; }
    public Receipt getReceipt() { return _receipt; }
    public void setReceipt(Receipt receipt) { this._receipt = receipt; }

public static class Receipt {
        private String _uid;
        private String _messages;
        private String _status;
        private int _code;
        private int _ttl;
        private String _url;

        public String getUid() { return _uid; }
        public void setUid(String uid) { this._uid = uid; }
        public String getMessages() { return _messages; }
        public void setMessages(String messages) { this._messages = messages; }
        public String getStatus() { return _status; }
        public void setStatus(String status) { this._status = status;}
        public int getCode() { return _code; }
        public void setCode(int code) { this._code = code; }
        public int getTtl() { return _ttl; }
        public void setTtl(int ttl) { this._ttl = ttl; }
        public String getUrl() { return _url; }
        public void setUrl(String url) { this._url = url; }
    }
}

My code to parse this response looks like this:-

ObjectMapper mapper = new ObjectMapper(); 
ReceiptResponse response = mapper.readValue(method.getResponseBodyAsStream(), ReceiptResponse.class);

But it throws the following error:-

12:03:44,195 ERROR [STDERR] Fatal transport error: No content to map to Object due to end of input
12:03:44,195 ERROR [STDERR] java.io.EOFException: No content to map to Object due to end of input
12:03:44,196 ERROR [STDERR]     at org.codehaus.jackson.map.ObjectMapper._initForReading(ObjectMapper.java:1630)
12:03:44,196 ERROR [STDERR]     at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:1580)
12:03:44,196 ERROR [STDERR]     at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1158)

Can anyone help me by explaining why this is happening? I thought perhaps it was because the messages are empty, but I tried putting dummy data in there and it still failed. I've looked for the ObjectMapper code and it seems to think it can't find the first token (am i correct?) but I can't see why it won't be able to find it?

any help would be appreciated!

asked Sep 23 '10 at 11:19
https://www.gravatar.com/avatar/178cb048c86a8dff74c3783e74003e70?s=32&d=identicon&r=PGthrow java.io.EOFException: No content to map to" />
rainyday
39117

22  
But how was it resolved!? –  andyb Oct 11 '11 at 14:51
11  
-1 because nothing is marked as answer, just "RESOLVED" comment. very selfish... –  Miroslav Hudak Aug 15 '12 at 11:43
1  
where is the answer ?? –  Punith Raj Jun 26 '13 at 8:25

8 Answers

Even I faced same issue in my Spring MVC project for REST call.The mistake in my code was I used @RequestBody in controller method for a GET call. I mapped the method for POST request and the issue is resolved.

answered Feb 5 '13 at 11:42
http://i.stack.imgur.com/9mYxV.jpg?s=32&g=1throw java.io.EOFException: No content to map to" />
Pramod
1187

I ran into this issue as well. In a Spring MVC HandlerInterceptorAdaptor that was meant to log all incoming HTTP requests, I had this code:

LOG.trace("[REQUEST / Body]: \n" + IOUtils.toString(request.getInputStream()));
LOG.trace("[REQUEST / Content-Type]: " + request.getContentType());

Unfortunately, by using getInputStream(), I would read to the end of the InputStream. Commenting out the first line fixed it. By the time the HTTP request reached my controller, it had reached the end of input as stated by the exception. Makes sense, my mistake!

answered Aug 8 '12 at 19:15
https://www.gravatar.com/avatar/4e8ea129f367d77233c954a114d8b6e9?s=32&d=identicon&r=PGthrow java.io.EOFException: No content to map to" />
Louis
1,7921725

I just ran into the same issue. I had an inordinately long stream from the response. Converting it to a String helped get past the initial exception. That didn't solve everything, though, so I ended up adding 0-arg constructors to all of the pojos involved in the mapping. The mapper worked perfectly after that.

answered Oct 20 '10 at 18:20
https://www.gravatar.com/avatar/7ff28379d958f59607b7c7b592052123?s=32&d=identicon&r=PGthrow java.io.EOFException: No content to map to" />
jrb
1

    
adding 0-arg constructor to the pojo? I don't understand... From the example above you have a json data, then.... how? @jrb –  gumuruh Apr 4 '12 at 19:54

I also faced the issue but the reason is just an empty string.

In case of we provide a empty string we get : java.io.EOFException: No content to map to Object due to end of input

To resolve this please validate json data before processing.

answered Aug 21 '12 at 9:23
https://www.gravatar.com/avatar/1e7fd2c049bd60f374428bd2e4cb2da1?s=32&d=identicon&r=PGthrow java.io.EOFException: No content to map to" />

I just got same error. After tracing, my issue was caused by the wrong parameter passed in javascript functions which caused undefined data was send to REST service.

logs as below, the line "Content-Length: 0" hint the root cause.

642 > PUT http://localhost:8080*
642 > Origin: http://localhost:8080
642 > X-Requested-With: XMLHttpRequest

answered Mar 19 '13 at 13:35
https://www.gravatar.com/avatar/09dad94a4cc01d7c7b8818a74a824e63?s=32&d=identicon&r=PGthrow java.io.EOFException: No content to map to" />
tss
314

I ran into the the issue, but I used curl to send to request.

The original command for data section is

-d "{'ID':123, 'Name':'ABC'}"

After I changed to

-d '{"ID":123, "Name":"ABC"}'

Then the problem was solved.

answered May 23 '13 at 16:51
https://www.gravatar.com/avatar/8d2a2d3c0af7616f6dc8878ff060ac39?s=32&d=identicon&r=PGthrow java.io.EOFException: No content to map to" />
Dino Tw
236212

I got the same issue because some of my JSON attributes had the value null. Removing the null values from the JSON completely got rid of the error.

answered Aug 29 '13 at 17:34
http://i.stack.imgur.com/dzgUp.jpg?s=32&g=1throw java.io.EOFException: No content to map to" />

I have acounter this issue when I send the request as the HTTP GET Method, after change the HTTP GET to HTTP POST Method, This problem is solved. You can try as I say, hope this will help you....

answered Dec 18 '13 at 7:41

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有