Spring MVC @RequestBody 400 Bad Request 问题? 400 报错
先上Controller代码:
package com.linewell.zzy.admin.action; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import com.linewell.service.mng.IArticleMng; import com.linewell.zzy.entity.Article; import com.linewell.zzy.toolkit.Readonly; @Controller public class AdminArticleAct { @Autowired private IArticleMng articleMng; @RequestMapping(value = "/admin/article/save.html", method = RequestMethod.POST) @ResponseBody public String save(@RequestBody Article article) ) { System.out.println("INININ"); return "OK"; } }spring配置代码:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="webBindingInitializer"> <bean class="com.linewell.zzy.core.springmvc.BindingInitializer"/> </property> <property name="messageConverters"> <list> <bean class = "org.springframework.http.converter.StringHttpMessageConverter"> <property name = "supportedMediaTypes"> <list><value>text/plain;charset=UTF-8</value></list> </property> </bean> <bean class = "org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> </bean> </list> </property> </bean> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> </bean> <context:annotation-config /> <mvc:annotation-driven /> <context:component-scan base-package="com.linewell.zzy.action,com.linewell.zzy.admin.action" /> <!-- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix"> <value>.jsp</value> </property> </bean> --> <!-- freemarker页面解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="prefix"> <value>/WEB-INF/</value> </property> <property name="suffix"> <value>.htm</value> </property> <property name="viewClass"> <value>org.springframework.web.servlet.view.freemarker.FreeMarkerView </value> </property> <property name="contentType" value="text/html;charset=UTF-8"></property> </bean> <!-- 如果模板不经常更新,此属性设置更新延迟时间 --> <!-- 配置Freemarker--> <bean id="freemarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPath"> <value></value> </property> <property name="freemarkerSettings"> <props> <prop key="template_update_delay">0</prop> <prop key="defaultEncoding">UTF-8</prop> </props> </property> </bean> <!-- 文件上传支持 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="5242880"/> </bean>--> <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <props> <prop key="org.springframework.web.bind.MissingServletRequestParameterException">error/requiredParameter</prop> <prop key="org.springframework.beans.TypeMismatchException">error/mismatchParameter</prop> <prop key="org.springframework.web.bind.ServletRequestBindingException">error/bindException</prop> <prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">error/maxUploadExceeded</prop> </props> </property> </bean> </beans>
页面代码:
<div class="loc clear"> <div class="l clear"> 当前位置:<a > <a > ${articleTypeObj.title}<#if formObj??>修改<#else>创建</#if> </div> <input type="button" class="btn r" value="返回列表"/> </div> <h1> 添加【${articleTypeObj.title}】文章 </h1> <form action="/admin/article/save.html" method="POST" id="form"> <table class="form"> <tr> <td class="t"> 文章标题: </td> <td> <input type="hidden" name="id" value="1"/> <input type="hidden" name="creatime" value="2012-01-12"/> <input type="hidden" name="creator"/> <input type="hidden" name="articleType"/> <input type="hidden" name="readCount"/> <input type="hidden" name="haveImg"/> <input type="text" name="title" /> </td> </tr> <tr> <td class="t"> 来源: </td> <td> <input type="text" name="source"/> </td> </tr> <tr> <td class="t"> 来源链接: </td> <td> <input type="text" name="sourceLink" /> </td> </tr> <tr> <td class="t"> 权限控制: </td> <td> <input type="text" /> </td> </tr> <tr> <td class="t"> 内容: </td> <td> <textarea id="content" style="width: 800px; height: 400px; visibility: hidden;"></textarea> <input id="contentInput" type="hidden" name="content"/> </td> </tr> <tr> <td colspan="2" align="center"> <input id="submit" class="btn" type="button" value="保存" /> <input id="" class="btn" type="button" value="保存并发布" /> <input id="" class="btn" type="button" value="预览" /> </td> </tr> </table> </form> <script> $("#submit").click(function(){ editor.sync(); $("#contentInput").val($("#content").val()); var article = $.toJSON($('#form').serializeObject()); var json='{"article":'+article+'}'; alert(json); jQuery.ajax( { type : 'POST', contentType : 'application/json', url : '/admin/article/save.html', processData : false, dataType : 'text', data : json, success : function(data) { alert("新增成功!"); }, error : function(XMLHttpRequest,textStatus,errorThrown) { alert(XMLHttpRequest.status); alert(XMLHttpRequest.readyState); alert(textStatus); } }); return false; }); </script>
js里的serializeObject()代码:
$.fn.serializeObject = function() { var o = {}; var a = this.serializeArray(); $.each(a, function() { if (o[this.name]) { if (!o[this.name].push) { o[this.name] = [ o[this.name] ]; } o[this.name].push(this.value || ''); } else { o[this.name] = this.value || ''; } }); return o; }所有代码如上,在执行提交数据时总是出现400 bad request错误,而进不了方法save,请问各位大侠这是怎么回事?谢了
另注:之前搭建框架的时候测试都没问题,现在不知道怎么回事,都搞了一天了
打开debug日志,查下原因######<bean id="jackson_hmc" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="objectMapper">
<bean class="org.codehaus.jackson.map.ObjectMapper">
<property name="dateFormat">
<bean class="java.text.SimpleDateFormat">
<constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss"></constructor-arg>
</bean>
</property>
</bean>
</property>
</bean>
把这个配置到AnnotationMethodHandlerAdapter 就解决了,还要下几个Jackson的包
都是水货啊,都不好用
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。