开发者社区> 问答> 正文

Spring MVC @RequestBody 400 Bad Request ?400报错

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,请问各位大侠这是怎么回事?谢了

另注:之前搭建框架的时候测试都没问题,现在不知道怎么回事,都搞了一天了



展开
收起
爱吃鱼的程序员 2020-06-05 12:01:05 764 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    打开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的包


    ######额,这个是政府项目,不能发######回复 @潘新遲 : debug日志发我######没效######最近我也遇到了这个问题,原因是表单提交的数据类型与Model中的数据类型不匹配。######Article的属性和你的form提交中的数据类型不 匹配###### 那居然是类型不匹配,如何让匹配啊,只能是字符串吗,我的实体类有int 和date类型,怎么办????######1 对象类型与model类型不一致
    2 model类型不能为private,应为protected/public
    ######没卵用######http://www.tuicool.com/articles/aYfaqa######你解决了吗?我也遇到这个问题###### @RequestBody  改成 ModelAttribute@######

    都是水货啊,都不好用

    2020-06-05 12:02:15
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第20讲):经典面试题与阿里等名企内部招聘求职面试技巧 立即下载
微服务架构模式与原理Spring Cloud开发实战 立即下载
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载

相关实验场景

更多