假设我已将JSON发布到服务器,如下所示:
{
warFile: {name: "test1", dependencies: [test0, test2]},
param: {build: true, test: true}
}
我有3节课如下:
public class WarFile{
private String name:
private String[] dependencies;
public void setName(){...};
public String getName(){...};
public void setDependencies(){...};
public String[] getDependencies(){...};
}
public class Param{
private boolean build;
private boolean test;
public void setBuild(){...};
public boolean isBuild(){...};
public void setTest(){...};
public boolean isTest(){...};
}
public class Command{
private WarFile warFile;
private Param param;
private void setWarFile(){...};
private WarFile getWarFile(){...};
private void setParam(){...};
private Param getParam(){...};
}
控制器如下:
@RequestMapping(value = "/test.ajax", method = RequestMethod.POST)
public @ResponseBody
BuildResult buildWar(@RequestBody Command cmd) {
return logic.build(cmd.getWarFile(), cmd.getParam());
}
由于Command中的warFile
和param
不是基本类型,我总是会收到以下错误:
The request sent by the client was syntactically incorrect.
如果Command中的所有属性都是基本类型,我确信没有任何问题。但是这个错误怎么会发生呢?我的意思是,对象很简单,不需要为它们编写自定义反序列化器。
答案 0 :(得分:0)
您的请求映射是否是/ html <form>
中的/test.ajax或/ test?
您是否添加了jackson mapper jar依赖项?如果是,下一个解决方案将逃脱你的报价我想。这可能有所帮助 Spring MVC : The request sent by the client was syntactically incorrect
答案 1 :(得分:0)
“发送给客户端的请求在语法上是不正确的”意味着您发送的请求的内容有问题,不一定是您的控制器或Spring配置。首先,您的JSON无效。杰克逊无法正确地将JSON映射到它应该代表的对象。它应该是:
{
"warFile": {
"name": "test1",
"dependencies": ["test0", "test2"]
},
"param": {
"build": true,
"test": true
}
}
其次,确保内容类型为application / json。最后,为什么你的Command对象有私有的getter / setter,你的setter没有字段参数?