使用json绑定发布Kendo UI和Spring MVC

时间:2014-01-04 15:23:36

标签: json spring-mvc kendo-ui

当我使用网格KendoUI时,SpringMVC和jackson存在问题。

实际上,网格数据源采用格式为:

的json
[{"name":"Apple","description":"a description","value":15}]

但是,SpringMVC将其序列化为:

{"name":"Apple","description":"a description","value":15}

在String中没有方括号,因此该网格不绑定值。

例如代码:

@RequestMapping(value="/product", method = RequestMethod.GET)
public @ResponseBody Product get(Model model) {
    Product app = new Product("Apple", "a description", 15);
    return app;
}

@RequestMapping(value="/product/json", method = RequestMethod.GET)
public @ResponseBody String getJson(Model model) {
    return"[{\"name\":\"Apple\",\"description\":\"a description\",\"value\":15}]";
}

3 个答案:

答案 0 :(得分:2)

您可以使用dataSource.schema.parse手动拦截并将表示您的JSON的字符串解析为有效的JavaScript数组。

答案 1 :(得分:0)

下面应该有效。 Kendo Grid期待一个JSON数组,因此块引用。

对于READ:

@RequestMapping(value = "/list", method = RequestMethod.GET)
public @ResponseBody
List<Product> list() {
      Product apple = new Product("Apple", "a description", 15);
      List<Product> listProduct = new ArrayList<Product>();
      listProduct.add(apple);
      return listProduct;
}

答案 2 :(得分:0)

@RequestMapping(value="/product", method = RequestMethod.GET)
public @ResponseBody Product[] get(Model model) {
    Product app = new Product("Apple", "a description", 15);
    return new Product[]{app};
}

    @RequestMapping(value="/product", method = RequestMethod.GET)
public @ResponseBody List<Product> get(Model model) {
    List<Product> product = new ArrayList<Product>();
    product.add(new Product("Apple", "a description", 15));
    product.add(new Product("Guice","a guice",3));
    return product;
}