在SpringMVC中使用@RequestBody和@ModelAttribute注解时遇到了很多问题,现记录下来。

  @ModelAttribute这个注解主要是将客户端请求的参数绑定参数到一个对象上,不用将很多参数@RequestParam或是@PathVariable。

但是在使用过程中遇到了问题,

第一次Html端请求代码如下,请求方式1

// 简单键值对
var data = {
productTypeIds : vproducttypeid,
channelTypeId : vchanneltypeid,
userId : 1,
}; // 提交保存
$.ajax({
"type" : "POST",
"url" : chainelUtil.URL.ACTION_IMPORT,
"data" : data,
"success" : function(data) {
bootbox.alertTimeout("创建成功!");
},
"error" : function(data) {
bootbox.alertTimeout("创建失败!");
}
});

Controller代码如下:

@RequestMapping(value = "/importstorestore", method = RequestMethod.POST)
@ResponseBody
public Object importStoreToStoreTree(@ModelAttribute StoreStoreRelationVO storeRelationVO ) {
}

StoreStoreRelationVO代码如下:

@Component
public class StoreStoreRelationVO implements Serializable{ /**
*
*/
private static final long serialVersionUID = 1L; // 渠道关系实体
private StoreStoreEntity storeStoreEntity; // 渠道类型Id
private String channelTypeId; // 商品分类Id字符串以","分割
private String productTypeIds; // 商品分类Id List
private List<String> productTypeIdList; // 区域Id,目前版本可能用不上
private String regionId; // 当前登陆用户
private String userId;
// 省略getter setter
}

StoreStoreEntity如下:

@Entity
@Table(name = "base_store_store")
public class StoreStoreEntity implements Serializable{ /**
*
*/
private static final long serialVersionUID = 1L; @Id
@Column
@GenericGenerator(name = "idGenerator", strategy = "increment")
@GeneratedValue(generator = "idGenerator")
private Long id; // 上级id
private Long upstoreid; // 下级id
private Long downstoreid; // 所属渠道id
private Long channelid;
// 省略getter 和 setter
}

如上的请求没问,服务器端可以接受到数据。

但是如果将请求的参数修改为如下,对象中组合对象,因为StoreStoreRelationVO 有个属性是StoreStoreEntity

// StoreStoreEntity
var storeStore = {
upstoreid : vupstoreid,
downstoreid:vcurrentstoreid,
channelid:1,
};
// StoreStoreRelationVO 
var data = {
storeStoreEntity:storeStore,
productTypeIds : vproducttypeid,
channelTypeId : vchanneltypeid,
userId : 1,
};

如果用请求方式1请求,直接报错

HTTP Status 404 -


type Status report

message

description The requested resource is not available.


Apache Tomcat/7.0.52

我将Ajax请求参数改为Json,如下,请求方式2

// 提交保存
$.ajax({
"type" : "POST",
"dataType" : 'json',
"contentType" : "application/json;charset=UTF-8",
"url" : chainelUtil.URL.ACTION_IMPORT,"data" : JSON.stringify(data),
"success" : function(data) {
var messagetext = $('#message');
messagetext.html("已经向系统成功提交申请,创建成功!");
bootbox.alertTimeout("创建成功!");
},
"error" : function(data) {
var messagetext = $('#message');
messagetext.html("服务器繁忙,请重新提交!");
bootbox.alertTimeout("创建失败!");
}
});

服务器端不变,这样不报错了,但是storeRelationVO却接收不到数据,不能自动装载。

我将Controller中的@ModelAttribute修改为@RequestBody

storeRelationVO可以正常接受到数据。

----------------------------------------------------------------------

为什么Json就能正确成功呢,我又将Ajax改回到最初格式,参数格式不设定

$.ajax({
"type" : "POST",
"url" : chainelUtil.URL.ACTION_IMPORT,
"data" : data,
"success" : function(data) {
bootbox.alertTimeout("创建成功!");
},
"error" : function(data) {
bootbox.alertTimeout("创建失败!");
}
});

结果不出所料,报错,415 Unsupported Media Type

HTTP Status 415 -


type Status report

message

description The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.


Apache Tomcat/7.0.52

看来用@RequestBody,需要传Json数据

----------------------------------

我将请求参数稍微改了一下,其余不变

var storeStore = {
upstoreid : vupstoreid,
downstoreid:vcurrentstoreid,
channelid:1,
}; var data = {
storeStoreEntity:storeStore,
productTypeIds : vproducttypeid,
channelTypeId : vchanneltypeid,
userid : 1,
};

结果报错,这个错我找了好久啊,最后发现一个大小写错误,userid应该对应实体中的userId,一个i应该是大写,所以请求参数不区分大小写!

HTTP Status 400 -


type Status report

message

description The request sent by the client was syntactically incorrect.


Apache Tomcat/7.0.52

SpringMVC---400错误The request sent by the client was syntactically incorrect ()的更多相关文章

  1. SpringMVC报错The request sent by the client was syntactically incorrect ()

    springmvc数据绑定出的错 在数据绑定的时候一定要主意Controller方法中的参数名和jsp页面里的参数名字是否一致或者按照绑定的规范来写, 如果不一致,可能回报如下错误: The requ ...

  2. HTTP Status 400 - description The request sent by the client was syntactically incorrect.

    HTTP Status 400 - type Status report message description The request sent by the client was syntacti ...

  3. 错误400-The request sent by the client was syntactically incorrect

    springMVC中,某个页面提交时报400错误,如下图.     解决方法: 1.在网上找了一下,答案是通常遇到这个错误是因为前端jsp页面的控件名称和controller中接收的参数名称不一致.但 ...

  4. 错误The request sent by the client was syntactically incorrect ()的解决

    http://www.cnblogs.com/xiandedanteng/p/4168609.html 这个错误是SpringMVC报出来的,见到它意味着html/jsp页面的控件名称 和 contr ...

  5. 错误:The request sent by the client was syntactically incorrect的解决

    问题: 错误400-The request sent by the client was syntactically incorrect. springMVC中,某个页面提交时报400错误,如下图. ...

  6. The request sent by the client was syntactically incorrect.

    HTTP Status 400 - type Status report message description The request sent by the client was syntacti ...

  7. 又见The request sent by the client was syntactically incorrect ()

    前几天遇到过这个问题(Ref:http://www.cnblogs.com/xiandedanteng/p/4168609.html),问题在页面的组件name和和注解的@param名匹配不对,这个好 ...

  8. "The request sent by the client was syntactically incorrect ()"问题定位及解决:

    Spring MVC "The request sent by the client was syntactically incorrect ()"解决办法: 把spring日志级 ...

  9. The request sent by the client was syntactically incorrect问题解决

    The request sent by the client was syntactically incorrect意思嘛,google翻译一下 通过日志不难看出,是由参数不匹配造成的. 所以对于Da ...

随机推荐

  1. Educational Codeforces Round 54 ---1076ABCDE

    1076A---Minimizing the String[字符串] http://codeforces.com/contest/1076/problem/A 题意: 删掉字符串中的一个字符使得得到的 ...

  2. 51Nod 1084:矩阵取数问题 V2(多维DP)

    1084 矩阵取数问题 V2  基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题  收藏  关注 一个M*N矩阵中有不同的正整数,经过这个格子,就能获得相应价值的奖励 ...

  3. hdu5173 How Many Maos Does the Guanxi Worth

    #include<bits/stdc++.h> using namespace std; #define INF 0x3f3f3f3f ]; ]; ][]; void dijkstra(i ...

  4. 命令提示符操作及Java的特点

    day1_3 命令提示符的操作 GUI 图形化方式(可视化) CLI 命令行方式 (编程方式) dir 列出当前目录下文件及文件夹 md 创建文件夹 rd 删除文件夹(只能删除空文件夹) cd 进入指 ...

  5. Mybatis(二,三)

    参考孤傲苍狼的博客,地址如下: http://www.cnblogs.com/xdp-gacl/p/4264301.html 在此声明,自己写博客,是为了学习总结过程中的记录.没有侵权和偷懒的意思. ...

  6. 使用fastjson解析数据后导致顺序改变问题

    在开发过程中遇到一个问题,服务器经过排序返回后的字符串数据使用fastjson解析后,数据顺序发生变化,引起业务异常. 解决办法: 1.解析时增加参数不调整顺序 JSONObject responde ...

  7. <--------------------------构造方法------------------------------>

    1 构造方法 初始化阶段 给对象的属性进行赋值 构造方法 什么是构造方法 : 字面 方法构建时 就使用的方法 对象创建的时候就使用的方法 作用:对象的属性值初始化2 如何用构造方法 修饰符 构造方法名 ...

  8. YAML Class ID Reference

    Classes Ordered by ID Number ID Class 1 GameObject 2 Component 3 LevelGameManager 4 Transform 5 Time ...

  9. Hi3516CV300 sample -> region

  10. day9大纲

    01 作业内容回顾 函数的初识: 封装一个功能. def 函数名(): 函数体 函数的返回值:return 1,结束函数. 2,返回给执行者(函数名())值. return ----> None ...