About java.io.EOFException exceptions for JSON to JAVA object

Source: Internet
Author: User
        has been working on the interaction of Spring 3.1.1 MVC + dojo1.8 to map the page's JSON data directly to Java objects through spring MVC. It's consistent with the following anomaly.
Java.io.EOFException:No content to map to Object due to end of input at Org.codehaus.jackson.map.objectmapper._initforre
	Ading (objectmapper.java:2775) at Org.codehaus.jackson.map.objectmapper._readmapandclose (ObjectMapper.java:2718) At Org.codehaus.jackson.map.ObjectMapper.readValue (objectmapper.java:1923) at Org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readInternal ( mappingjacksonhttpmessageconverter.java:124) at Org.springframework.http.converter.AbstractHttpMessageConverter.read (abstracthttpmessageconverter.java:153) at Org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConve Rters (abstractmessageconvertermethodargumentresolver.java:120) at Org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConve Rters (abstractmessageconvertermethodargumentresolver.java:91) at Org.springframework.web.servlet.mvc.method.annotation.RequestResponsebodymethodprocessor.resolveargument (requestresponsebodymethodprocessor.java:71) at Org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument ( HANDLERMETHODARGUMENTRESOLVERCOMPOSITE.JAVA:75) at Org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues ( invocablehandlermethod.java:156) at Org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest (invocablehandlermethod.java:117 ) at Org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle ( servletinvocablehandlermethod.java:96) at Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod ( requestmappinghandleradapter.java:617) at Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal ( requestmappinghandleradapter.java:578) at Org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle (AbstracthandleRMETHODADAPTER.JAVA:80) at Org.springframework.web.servlet.DispatcherServlet.doDispatch (Dispatcherservlet.java : 923) at Org.springframework.web.servlet.DispatcherServlet.doService (dispatcherservlet.java:852) at Org.springframework.web.servlet.FrameworkServlet.processRequest (frameworkservlet.java:882) at Org.springframework.web.servlet.FrameworkServlet.doGet (frameworkservlet.java:779) at Javax.servlet.http.HttpServlet.service (httpservlet.java:621) at Javax.servlet.http.HttpServlet.service ( httpservlet.java:723)

After multiple tracking and debugging, this exception was found because dojo passed the JSON data to spring MVC as NULL, so it was not able to turn.

After careful examination of the code, it is discovered that the AJAX request sent with Get is the following code:

XHR ("/springmvcrestful/login", {
				data:JSON.stringify (userInfo),
				//query:json.stringify (UserInfo),// Add this to the URL display parameter
				sync:true,
				handleas: "JSON",
				timeout:2000,
				headers: {
					' content-type ': ' Application/json;charset=utf-8 '
				}
			}). Then (function (response) {

Need to modify to send POST request:

Xhr.post ("/springmvcrestful/login", {
				data:JSON.stringify (userInfo),
				//query:json.stringify (UserInfo), Add this to the URL display parameter
				sync:true,
				handleas: "JSON",
				timeout:2000,
				headers: {
					' content-type ': ' Application/json;charset=utf-8 '
				}
			}). Then (function (response) {
At the same time, spring's controller is also post:

@RequestMapping (value = "/login", method = requestmethod.post) public
	@ResponseBody Jsonresult Login (@RequestBody User user,
			httpservletrequest request, httpservletresponse response) {
		log.info (user.tostring ());
		Request.getsession (). setattribute (Constants.user, USER);
		return new Jsonresult (true, ' return OK ');
	}

Finally done.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.