访问Controller返回400BadRequest
问题
SpringMVC使用自定义类型接收参数时, form提交会返回400 Bad Request,controller接口里的方法没有得到调用。
1
2
3
4
5
6
7
8
9
10
11
12
|
@RequestMapping (value = "/list.do" , method = {RequestMethod.GET , RequestMethod.POST}) public String list(Order param, HttpServletRequest request) { logger.info(JSONObject.fromObject(param).toString()); Integer count = orderService.findCount(param); logger.info(count); param.setRows(count); List<Order> result = orderService.findByPage(param); logger.info(JSONArray.fromObject(result).toString()); request.setAttribute( "param" , param); request.setAttribute( "result" , result); return "order/list" ; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
< form action="<%=basePath%>order/list.do" id="myform" method="post" > < input type = "hidden" name = "currentPage" value = "${param.currentPage}" /> < table > < tr > < td width = "80" align = "right" >订单状态: </ td > < td > < select name = "order_status" autoWidth = "true" style = "width:120px;" > < option value = "" >全部</ option > < c:forEach items = "${order_status_map}" var = "temp" > < option value = "${temp.key}" <c:if test = "${temp.key == param.order_status}" >selected="selected"</ c:if >>${temp.value}</ option > </ c:forEach > </ select > </ td > < td align = "right" >订单号: </ td > < td > < input type = "text" name = "order_no" id = "order_no" onfocus = "this.value=''" value = "${param.order_no}" style = "width:140px;" /> </ td > </ tr > < tr > < td width = "80" align = "right" >创建时间: </ td > < td >< input type = "text" class = "date" name = "createTimeStart" value = "${param.createTimeStart}" style = "width:120px;" /></ td > < td align = "center" > 至 </ td > < td >< input type = "text" class = "date" name = "createTimeEnd" value = "${param.createTimeEnd}" style = "width:120px;" /></ td > < td >< button >< span class = "icon_find" >查询</ span ></ button ></ td > < td >< button onclick = "add();return false;" >< span class = "icon_add" >添加</ span ></ button ></ td > </ tr > </ table > </ form > |
直接在浏览器地址栏输入<%=basePath%>order/list.do是可以访问的并查询出数据的,我一开始以为是get方法能访问,post不能访问,后来发现,get使用Context-Type为application/x-www-form-urlencoded时也会访问不了,post使用使用application/json也能访问。
所以,这就需要修改form里的enctype属性为application/json,但是enctype属性只支持下面表格三种,
HTML form 标签的 enctype 属性
值 | 描述 |
---|---|
application/x-www-form-urlencoded | 在发送前编码所有字符(默认) |
multipart/form-data | 不对字符编码。在使用包含文件上传控件的表单时,必须使用该值。 |
text/plain | 空格转换为 “+” 加号,但不对特殊字符编码。 |
我试了下,text/plain和application/json具有同样的效果。
虽然在form里加上enctype=”text/plain”可以解决接口返回400BadRequest的问题,但是有新的问题出现:接口的参数Order param接受不到post提交的值,但是将参数直接拼接在url,Order param则可以接收到值,Order param
SpringMVC遇到的Http400BadRequest总结
在搭建SpringMVC环境,在使用中遇到了多次Bad Request的连接
下面来总结下
1.参数类型不对,如后台实体类的属性为int,但传来的参数为字符串
2.因为我的粗心,本来是要通过Ajax获得一个json串,但是后台的方法在返回值类型前没有加@RequestBody注解
3.传递的参数为日期,spring不知道该以什么格式转换为Date类型,解决办法为在实体类的日期属性上加上@DateTimeFormat(pattern="yyyy-MM-dd")注解即可
4.刚遇到的问题,排查了好久终于知道是什么原因了,实体类中有int类型的属性,但是前台传参时传递的样子是这样的"age=",并没有给一个准确的数字(age=18),所以导致Spring在对int型属性进行赋值时无法正确赋值
以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/u012643122/article/details/76269744