对象未从AJAX绑定到Spring控制器

时间:2018-09-06 15:59:01

标签: jquery ajax spring-mvc spring-boot

我正在尝试使用jquery ajax将数据从javascript传递到spring控制器。

var inputParam = {
  name: "xxx",
  address: {
    city: "chicago",
    zip: "123456"
  }
};

var jqxhr = $.ajax({
  url: "/myurl"+ $.param(inputparam),
  type: "GET",
  dataType: "html"               
});

控制器

@RequestMapping(value = "/myurl", method = RequestMethod.GET)
 public String getMyPage(  @ModelAttribute InputParams inputParam,HttpServletRequest request, Model model ) throws Exception{
        //my code
  }

pojo

class InputParams  
{
  private String name;
  private Address address;
  //getter setter
}

class Address 
{
  private String city;
  private String zip;
  //getter setter
}

但这无法正常工作,并且出现错误

  

org.springframework.beans.InvalidPropertyException:Bean类[InputParams]的无效属性“ address [city]”:在索引属性路径“ address [city]”中引用的属性既不是数组也不是List也不是Map;返回值是[城市]

0 个答案:

没有答案