Spring mvc @RequestBody未绑定复杂对象
我试图使用Spring mvc @RequestBody未绑定复杂对象,spring-mvc,spring-boot,Spring Mvc,Spring Boot,我试图使用jqueryajax将json对象表单javascript传递给Spring控制器。但如果json对象很复杂,它就没有绑定 Javascript: var inputParam={}; inputParam.name="xxx"; inputParam.address.city="chicago"; inputParam.address.zip="123456"; var jqxhr = $.ajax({ url:"/m
jqueryajax
将json
对象表单javascript
传递给Spring控制器。但如果json
对象很复杂,它就没有绑定
Javascript:
var inputParam={};
inputParam.name="xxx";
inputParam.address.city="chicago";
inputParam.address.zip="123456";
var jqxhr = $.ajax({
url:"/myurl",
type:"POST",
dataType: "html",
contentType :'application/json',
data:JSON.stringify(inputparam),
beforeSend: function( xhr ) {
}
});
//Handle a successful call to data service
jqxhr.done(function( data, textStatus, jqxhr,response ) {
// my successful code handle
});
//Handle an unsuccessful call to data service
jqxhr.fail(function(jqXHR, textStatus) {
//my error handling code
});
@RequestMapping(value = "/myurl", method = RequestMethod.POST)
public String getMyPage( @RequestBody InputParams inputParam,HttpServletRequest request, Model model ) throws Exception{
//my code
}
class InputParams {
private String name;
private Address address;
//getter setter
}
class Address{
private String city;
private String zip;
//getter setter
}
控制器:
var inputParam={};
inputParam.name="xxx";
inputParam.address.city="chicago";
inputParam.address.zip="123456";
var jqxhr = $.ajax({
url:"/myurl",
type:"POST",
dataType: "html",
contentType :'application/json',
data:JSON.stringify(inputparam),
beforeSend: function( xhr ) {
}
});
//Handle a successful call to data service
jqxhr.done(function( data, textStatus, jqxhr,response ) {
// my successful code handle
});
//Handle an unsuccessful call to data service
jqxhr.fail(function(jqXHR, textStatus) {
//my error handling code
});
@RequestMapping(value = "/myurl", method = RequestMethod.POST)
public String getMyPage( @RequestBody InputParams inputParam,HttpServletRequest request, Model model ) throws Exception{
//my code
}
class InputParams {
private String name;
private Address address;
//getter setter
}
class Address{
private String city;
private String zip;
//getter setter
}
POJO:
var inputParam={};
inputParam.name="xxx";
inputParam.address.city="chicago";
inputParam.address.zip="123456";
var jqxhr = $.ajax({
url:"/myurl",
type:"POST",
dataType: "html",
contentType :'application/json',
data:JSON.stringify(inputparam),
beforeSend: function( xhr ) {
}
});
//Handle a successful call to data service
jqxhr.done(function( data, textStatus, jqxhr,response ) {
// my successful code handle
});
//Handle an unsuccessful call to data service
jqxhr.fail(function(jqXHR, textStatus) {
//my error handling code
});
@RequestMapping(value = "/myurl", method = RequestMethod.POST)
public String getMyPage( @RequestBody InputParams inputParam,HttpServletRequest request, Model model ) throws Exception{
//my code
}
class InputParams {
private String name;
private Address address;
//getter setter
}
class Address{
private String city;
private String zip;
//getter setter
}
我遇到的问题是地址对控制器没有约束力
你知道我做错了什么吗?这样设置你的
inputParam
就可以解决你的问题了
var inputParam={
name : "xxx",
address: {
city :"chicago",
zip :"123456"
}
};
请在InputParams类中提供正确类型的参数“name”。JS代码抛出。您的Java代码无法编译。发布真实代码。我不能把我所有的控制器代码。这也不是必须的。我的问题是,从ajax传递的inputParam在控制器端没有绑定。如果这是您的真实代码,您的ajax请求甚至无法发送。发布真实代码。当数据发布到后端或仅
null
时,是否会引发错误或异常?