关于JSON to JAVA object的java.io.EOFException异常

        今天一直在弄Spring 3.1.1 MVC + dojo1.8的交互问题,想把页面的JSON数据通过Spring MVC直接映射到Java对象,却一致出现下面这个异常
java.io.EOFException: No content to map to Object due to end of input
	at org.codehaus.jackson.map.ObjectMapper._initForReading(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.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:120)
	at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(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)

经过多次跟踪和调试,发现出现此异常的原因是dojo传给Spring MVC的JSON数据为空,所以转不了,就出现这异常了。

仔细检查代码以后,发现是用get发送的ajax请求,如下代码:

 xhr("/SpringMVCRestful/login", {
				data : JSON.stringify(userInfo),
				//query: JSON.stringify(userInfo), //加这个会在url显示参数
				sync : true,
				handleAs : "json",
				timeout : 2000,
				headers : {
					'Content-Type' : 'application/json;charset=utf-8'
				}
			}).then(function(response) {

需要修改成发送post请求:

xhr.post("/SpringMVCRestful/login", {
				data : JSON.stringify(userInfo),
				//query: JSON.stringify(userInfo), //加这个会在url显示参数
				sync : true,
				handleAs : "json",
				timeout : 2000,
				headers : {
					'Content-Type' : 'application/json;charset=utf-8'
				}
			}).then(function(response) {
同时Spring的controller也配成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");
	}

终于搞定。

你可能感兴趣的:(异常解决)