'In Spring mvc application, @RequestBody is throwing java.io.EOFException
In the jsp i am passing the data as json,as in the controller method i am trying to map the data to Personbean object using @RequestMapping. But its throwing an exception.
@ResponseBody is working fine, if i remove the @ResponseBody code. Please suggest what am i doing wrong? I have pasted the code for reference.
//jsp (added the data as json and also content type)
<script>
function abc(){
alert('here!!');
$.ajax({
url:'anki',
type:'GET',
data:({name:"me"}),
dataType: 'json',
contentType: 'application/json',
success:function(data){
alert('here gain');
$('#time').html(data.name);
}
});
}
</script>
<input type="button" name="submit" title="submit" onclick="abc()"/>
<div id="time"></div>
//PersonBean (Bean class)
package com.controller;
public class Personbean {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
// controller method
//(Trying to use both @ResponseBody and @RequestBody in this method)
@RequestMapping(value="/anki",method=RequestMethod.GET)
public @ResponseBody EmployeeBean hhh(@RequestBody Personbean pb){
System.out.println(pb.getName());
System.out.println("new method");
EmployeeBean e=new EmployeeBean();
e.setId("1001");
e.setName("ankita");
return e;
}
//i have added the "jacksonMessageConverter" in the dispatcher servlet
//dispatcher servlet
<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jacksonMessageConverter"/>
</list>
</property>
//Exception stacktrace(For reference)
java.io.EOFException: No content to map to Object due to end of input
at org.codehaus.jackson.map.ObjectMapper._initForReading(ObjectMapper.java:2444)
at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2396)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1662)
at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readInternal(MappingJacksonHttpMessageConverter.java:124)
at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:154)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.readWithMessageConverters(HandlerMethodInvoker.java:633)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveRequestBody(HandlerMethodInvoker.java:597)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:346)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:669)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:574)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:498)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:394)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Solution 1:[1]
Try this
$.ajax({
url:'anki',
type:'POST',
data:JSON.stringify(eval({"name":"me"})),
dataType: 'json',
contentType: 'application/json',
success:function(data){
alert('here gain');
// $('#time').html(data.name);
}
});
Dont forget to change the Method to POST in your controller
method=RequestMethod.POST
Solution 2:[2]
The GET method will not work because it will send the data in the URL and not in the request body. @RequestBody expects data to be present in body of request. Changing the http method to POST will resolve the error.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Kris |
Solution 2 | Saurabh Kutwal |