今天一直在弄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");
	}

终于搞定。

GitHub 加速计划 / js / json
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:1 个月前 )
960b763e 4 个月前
8c391e04 6 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐