`
cn_arthurs
  • 浏览: 321568 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

奇怪的400 BAD REQUEST

阅读更多

    项目以Spring MVC + Hibernate 为后台,前台通过Jquery的AJAX对象,向后台POST一个JSON对象,Spring MVC的Controller方法通过@RequestBody自动转换成POJO对象。

 

在测试的时候,通过httpwatch发现前台总是收到Error 400 BAD_REQUEST错误信息,并且请求时content-length始终为0.



 

而后台的日志则显示如下:

2013-12-31 09:47:46,700 DEBUG [13180593@qtp-459327-2] (DispatcherServlet.java:823) - DispatcherServlet with name 'SpringServlet' processing POST request for [/ma/um/saveuser.do]
2013-12-31 09:47:46,700 DEBUG [13180593@qtp-459327-2] (AbstractHandlerMethodMapping.java:220) - Looking up handler method for path /um/saveuser.do
2013-12-31 09:47:46,700 DEBUG [13180593@qtp-459327-2] (AbstractHandlerMethodMapping.java:227) - Returning handler method [public com.zhiqiang.ma.um.entity.Person com.zhiqiang.ma.um.controller.UserController.saveuser(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse,com.zhiqiang.ma.um.entity.Person)]
2013-12-31 09:47:46,701 DEBUG [13180593@qtp-459327-2] (AbstractBeanFactory.java:246) - Returning cached instance of singleton bean 'userController'
2013-12-31 09:47:46,701 DEBUG [13180593@qtp-459327-2] (AbstractMessageConverterMethodArgumentResolver.java:140) - Reading [class com.zhiqiang.ma.um.entity.Person] as "application/json;charset=UTF-8" using [org.springframework.http.converter.json.MappingJacksonHttpMessageConverter@e9fc96]
2013-12-31 09:47:46,702 DEBUG [13180593@qtp-459327-2] (AbstractHandlerExceptionResolver.java:132) - Resolving exception from handler [public com.zhiqiang.ma.um.entity.Person com.zhiqiang.ma.um.controller.UserController.saveuser(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse,com.zhiqiang.ma.um.entity.Person)]: org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: No content to map to Object due to end of input; nested exception is java.io.EOFException: No content to map to Object due to end of input
2013-12-31 09:47:46,705 DEBUG [13180593@qtp-459327-2] (AbstractHandlerExceptionResolver.java:132) - Resolving exception from handler [public com.zhiqiang.ma.um.entity.Person com.zhiqiang.ma.um.controller.UserController.saveuser(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse,com.zhiqiang.ma.um.entity.Person)]: org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: No content to map to Object due to end of input; nested exception is java.io.EOFException: No content to map to Object due to end of input
2013-12-31 09:47:46,706 DEBUG [13180593@qtp-459327-2] (AbstractHandlerExceptionResolver.java:132) - Resolving exception from handler [public com.zhiqiang.ma.um.entity.Person com.zhiqiang.ma.um.controller.UserController.saveuser(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse,com.zhiqiang.ma.um.entity.Person)]: org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: No content to map to Object due to end of input; nested exception is java.io.EOFException: No content to map to Object due to end of input
2013-12-31 09:47:46,708 DEBUG [13180593@qtp-459327-2] (DispatcherServlet.java:999) - Null ModelAndView returned to DispatcherServlet with name 'SpringServlet': assuming HandlerAdapter completed request handling
2013-12-31 09:47:46,708 DEBUG [13180593@qtp-459327-2] (FrameworkServlet.java:966) - Successfully completed request

 

根据日志分析得出,jquery并没有将数据发送到MappingJacksonHttpMessageConverter,所以才会报错。

 

多天以来度娘、谷哥给予了很大的帮助,后来发现原来是httpwatch在捣乱,将httpwatch关闭后,问题解决了。附代码仅供参考。

1.后台XML配置

<mvc:annotation-driven />
	
	
	<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
	
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
		<property name="messageConverters">
			<list>
				<ref bean="mappingJacksonHttpMessageConverter"/>
			</list>
		</property>
	</bean>
	 
	<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
		<property name="supportedMediaTypes">
			<list>
				<value>text/html;charset=UTF-8</value>
				<value>application/json;charset=UTF-8</value>
				<value>application/x-www-form-urlencoded</value>
			</list>
		</property>
	</bean>

 2.Contorller方法

@RequestMapping(value="/um/saveuser.do",method = RequestMethod.POST)
	public  @ResponseBody Person saveuser(HttpServletRequest request,HttpServletResponse response,@RequestBody Person user){
		log.debug(user.toString());
		return user;
	}

 3.前台代码

<%@ page language="java" contentType="text/html;charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="../script/jquery-1.10.2.min.js"></script>
<script>
var cfg = {
        type: 'POST', 
        data: JSON.stringify({userName:'测试用户',password:'password'}), 
        dataType: 'json',
        contentType:'application/json;charset=UTF-8',        
        success: function(result) { 
            alert(result); 
        } 
    };

function doTestJson(actionName){
    cfg.url = actionName;
    $.ajax(cfg);
}
</script>
</head>
<body>
	<a href="#" onClick="doTestJson('saveuser.do');">jsonlogin</a>

</body>
</html>

 

参考:

  http://bugs.jquery.com/ticket/12790

 

 

 

  • 大小: 53.8 KB
分享到:
评论
1 楼 wps886 2014-08-15  
google了半天全是错的,只有楼主的xml配置有用,太感谢了!!!

相关推荐

    nginx服务器access日志中大量400 bad request错误的解决方法

    主要介绍了nginx服务器access日志中大量400 bad request错误的解决方法,本文结论是空主机头导致的大量400错误日志,关闭默认主机的日志记录就可以解决问题,需要的朋友可以参考下

    Postman 接口测试

    接口测试是后端,甚至客户端常用的手段。 但是,并不是谁都了解 postman 的...

    loadrunne11之VuGen相关技术

    百度上发现的一篇不错的 loadrunner使用文档, 个人感觉 比较详细涵盖了loadrunner 录制选项设置Recording Options General Option,以及关联、参数化、检查点、集合点的设置等。

    基于python 爬虫爬到含空格的url的处理方法

    在某网站主页提取url进行迭代,爬虫请求主页时没有问题,返回正常,但是在访问在主页提取到的url时出现了400状态码(400 Bad Request)。 结论 先贴出结论来,如果url里有空格的话,要把空格替换成 ,也就是url编码,...

    Ajax报错400的参考解决办法

    Failed to load resource: the server responded with a status of 400 (Bad Request) 报错代码为400的错误的原因:基本上都是前台传的数据格式不正确造成的,但是这个格式如何不正确,就得看如何理解这个数据到底是...

    Badboy

    BadboyInstaller-2.0.5.exe Badboy监控internet explorer的活动,提供...继续录制脚本. Remark:必须等到上一步的request处理完毕后,才能点击发送下一个request,否则下一个request会作为第一个request的子frame.

    AccessLog_scan_badRequest

    scan bad request from Access_log About vim config.py for simply configures, 首先先配置些个参数,比如 + 需要扫描的日志文件 + 日志格式 + 输出记录文件 + 报警百分比 + url预处理条件 and then run 然后走着 ...

    Discuz Bad Request (Invalid Hostname)问题解决方法(Discuz 和DX都适用)

    俺在做手机团购站用户登录退出的时候老提示Bad Request (Invalid Hostname)经过多次折腾,服务器,DNS,IIS都设置没有问题,域名设置也没有问题,但是还是提示Bad Request (Invalid Hostname)

    badboy中文教程

    badboy中文教程

    linux上squid设置透明代理教程

    英文的哈,嘿嘿,想看中文的下了别骂我,这个教程是从tldp上搞下来的,很详细

    Badboy Badboy Badboy Badboy Badboy Badboy

    Badboy Badboy Badboy Badboy Badboy Badboy Badboy Badboy Badboy Badboy Badboy Badboy Badboy Badboy Badboy Badboy Badboy Badboy Badboy Badboy Badboy Badboy Badboy Badboy Badboy

    国标gb28181 抓包

    国标信令抓包, 可以查看自己的信令是否使用正确,

    最新 badboy自动化测试工具

    最新 badboy自动化测试工具最新 badboy自动化测试工具最新 badboy自动化测试工具最新 badboy自动化测试工具最新 badboy自动化测试工具最新 badboy自动化测试工具最新 badboy自动化测试工具最新 badboy自动化测试工具...

    badusb.zip_bad usb_badusb_badusb下载_badusb制作教程_badusb脚本开发

    badusb制作工具包,需要自行打包带走

    badusb_badusb_

    badusb资料,需要自行打包带走

    badboy中文手册

    Badboy也是一个强大的测试工具,是用C++开发的,被设计用于测试和开发复杂的动态应用。Badboy功能丰富(包括一个捕获/重播接口,强大的压力测试支持,详细的报告、图形)使得测试和开发更加容易。 官方网址:...

    Bad Piggies 代码&素材 BP2.3.6 音频源素材

    Bad Piggies 代码&素材 BP2.3.6 音频源素材Bad Piggies 代码&素材 BP2.3.6 音频源素材Bad Piggies 代码&素材 BP2.3.6 音频源素材Bad Piggies 代码&素材 BP2.3.6 音频源素材Bad Piggies 代码&素材 BP2.3.6 音频源素材...

    bad apple 图片资源包

    bad apple图片资源,25fps,320*240

    Linux服务器nginx访问日志里出现大量http 400错误的请求分析

    主要介绍了Linux服务器nginx访问日志里出现大量http 400错误的请求分析,需要的朋友可以参考下

    Badboy2.2.5.zip

    Badboy是专门录制测试脚本的工具,Badboy录制脚本的方法比较方便易懂,Badboy-2.2.5是最新版可以配合Jmeter使用,快速录制脚本且可以保存为JMeter支持的脚本。badboy测试工具是一个强大WEB测试工具,badboy测试工具...

Global site tag (gtag.js) - Google Analytics