1

this error I got during GET method implementation

java.io.EOFException: No content to map to Object due to end of input

my controller

   @RequestMapping(value = "/Login.htm", method = RequestMethod.GET,consumes="application/json",produces="application/json")
    public @ResponseBody Map<String, Object> Login(HttpServletRequest request,@RequestBody UserInput user) {

        Map<String, Object> modelMap = new HashMap<String, Object>(1);
        modelMap.put("status",userManagerDAO.LoginUser(user));
        return modelMap;

    }

Its working with post but not working with get method.

please help me to make this method get my input json.

{
"cusId":1,
"loginId" : "123ASDF",
"password": "test123"
}
jayesh
  • 2,233
  • 6
  • 40
  • 75

2 Answers2

1

Get Request will not have body. For get to make it work you can include your json object in URL as parameter, Consume that parameter then use JSON parser to parse it into the object.

some thing like:
http:///Login.htm?input="{"cusId":1,"loginId" : "123ASDF","password": "test123"}

don't forget to encode the URL.and then

public Map<String, Object> Login(HttpServletRequest 
request,@RequestParam(value="input")String userInput) {
    // Convert userInput string to UserInput object, use Jackson.
    Map<String, Object> modelMap = new HashMap<String, Object>(1);
    modelMap.put("status",userManagerDAO.LoginUser(user));
    return modelMap;

}

PS: sending of JSON objects via URL just to support GET is not advisable. POST is the right way to do it. Because of
1. Security if you are using https no one can see whats in req body.
2. URL length is limited, if your json is very large then the Get will break.

shreyas K N
  • 145
  • 2
  • 11
  • I not like this way of implementation because I not wont to pass any parameter in URL – jayesh Mar 25 '14 at 10:03
  • Agree, but the server semantics is not usually implemented in a way where GET has a body. Although theoratically possible, majority dont use it this way. https://groups.yahoo.com/neo/groups/rest-discuss/conversations/topics/9962 – shreyas K N Mar 25 '14 at 10:04
  • Well, spring does permit GET with body and the question is about spring. – soulcheck Mar 25 '14 at 10:06
  • Basically this isn't the problem with how the endpoint is written - OPs java code is completely legal. The problem is on the client. – soulcheck Mar 25 '14 at 10:06
  • I doubt if Spring allows it let me check it once. Also please refer to this post http://stackoverflow.com/questions/978061/http-get-with-request-body – shreyas K N Mar 25 '14 at 10:08
  • Tried with Spring with request body as string with GET request. Although Spring compilation is going through, at runtime the request body is not filled. tested with Rest Client. – shreyas K N Mar 25 '14 at 10:18
  • @shreyasKN yeah, it's a problem with the client (in this case - the browser). if you try curling to the endpoint and you'll see that request it ok. Your advice is ok too, just wanted to make clear the reason why it doesn't work. – soulcheck Mar 25 '14 at 10:33
  • @Jayesh, Sorry i have no experience in jquery ajax. will let you know when i try it. – shreyas K N Mar 26 '14 at 12:15
0

This is a limitation of RestClient and possibly many, if not all, XMLHttpRequest implementations.

Basically if you try curling to your service

curl -XGET "localhost:8080/myapp/Login.htm" -H"Content-Type: application/json" -d '{
    "cusId":1,
    "loginId" : "123ASDF",
    "password": "test123"
}'

You will see that your endpoint works correctly.

The problem is that apparently XMLHttpRequest implementation in Firefox (and Chrome) doesn't support sending bodies with GET requests.

So you're left with either POSTing it, encoding your body as request parameter like @shreyasKN suggested or putting a proxy in between that translates requests for you.

soulcheck
  • 34,060
  • 6
  • 82
  • 89
  • hi this can work with any test tools like Jmeter or rest service tools and also in java resthttpclient ? – jayesh Mar 26 '14 at 05:45
  • @jayesh jmeter doesn't support it. Apache HttpClient doesn't by default, but you can force it to in a way shown in [this answer](http://stackoverflow.com/a/12535806/642340) – soulcheck Mar 26 '14 at 08:29
  • In general you can test any tool you're using by running `nc -l ` and then sending requests to that port. `nc` will print the whole request to the console – soulcheck Mar 26 '14 at 08:30