SpringMVC數據綁定實例

接收bean

@RequestMapping(value = "/test/query",method = RequestMethod.GET)
    @ResponseBody
    public QueryParam getQueryByBean(QueryParam param, Order order) throws JsonProcessingException {
        System.out.println(objectMapper.writeValueAsString(order));
        return param;
    }

無需額外註解,能夠自動填充bean屬性,其中bean支持嵌套對象,嵌套list對象,嵌套list簡單類型,嵌套array簡單類型java

自動填充簡單屬性

http://localhost:8080/test/query?id=1&count=3&name=111

自動填充多個bean

代碼git

@RequestMapping(value = "/test/query",method = RequestMethod.GET)
    @ResponseBody
    public QueryParam getQueryByBean(QueryParam param, Order order) throws JsonProcessingException {
        System.out.println(objectMapper.writeValueAsString(order));
        return param;
    }

請求github

http://localhost:8080/test/query?id=1&count=3&name=111&cost=99

輸出ajax

{
  "id": "1",
  "name": "111",
  "desc": null,
  "finished": null,
  "count": 3,
  "stringList": null,
  "arrs": null,
  "address": null,
  "orders": null,
  "orderArrs": null
}

另一個bean的值spring

{
  "name": "111",
  "cost": 99
}

同名的屬性,多個bean會被填充爲同一個值,不一樣名的會自動去其餘bean裏頭尋找json

自動填充嵌套對象屬性

http://localhost:8080/test/query?id=1&address.name=xxx&address.district=111

輸出segmentfault

{
  "id": "1",
  "name": null,
  "desc": null,
  "finished": null,
  "count": null,
  "stringList": null,
  "arrs": null,
  "address": {
    "name": "xxx",
    "district": "111"
  },
  "orders": null,
  "orderArrs": null
}

自動填充簡單類型array

http://localhost:8080/test/query?arrs[0]=1&arrs[1]=2

輸出數組

{
  "id": null,
  "name": null,
  "desc": null,
  "finished": null,
  "count": null,
  "stringList": null,
  "arrs": [
    "1",
    "2"
  ],
  "address": null,
  "orders": null,
  "orderArrs": null
}

自動填充簡單類型list

http://localhost:8080/test/query?stringList[0]=1&stringList[1]=2

輸出mvc

{
  "id": null,
  "name": null,
  "desc": null,
  "finished": null,
  "count": null,
  "stringList": [
    "1",
    "2"
  ],
  "arrs": null,
  "address": null,
  "orders": null,
  "orderArrs": null
}

自動填充array對象

http://localhost:8080/test/query?id=1&orderArrs[0].name=xxx&orderArrs[0].cost=100&orderArrs[1].name=yyy&orderArrs[1].cost=99.9

輸出app

{
  "id": "1",
  "name": null,
  "desc": null,
  "finished": null,
  "count": null,
  "stringList": null,
  "arrs": null,
  "address": null,
  "orders": null,
  "orderArrs": [
    {
      "name": "xxx",
      "cost": 100
    },
    {
      "name": "yyy",
      "cost": 99.9
    }
  ]
}

自動填充list對象

http://localhost:8080/test/query?id=1&orders[0].name=xxx&orders[0].cost=100&orders[1].name=yyy&orders[1].cost=99.9

輸出

{
  "id": "1",
  "name": null,
  "desc": null,
  "finished": null,
  "count": null,
  "stringList": null,
  "arrs": null,
  "address": null,
  "orders": [
    {
      "name": "xxx",
      "cost": 100
    },
    {
      "name": "yyy",
      "cost": 99.9
    }
  ],
  "orderArrs": null
}

接收array

/**
     * http://localhost:8080/test/direct/array?content=111,222,444
     * @param content
     * @return
     */
    @RequestMapping(value = "/test/direct/array",method = RequestMethod.GET)
    @ResponseBody
    public String[] getDirectArray(String[] content){
        return content;
    }

    /**
     * http://localhost:8080/test/direct/array-obj?content=111,222
     * @param content
     * @return
     */
    @RequestMapping(value = "/test/direct/array-obj",method = RequestMethod.GET)
    @ResponseBody
    public Address[] getDirectArrayObject(@RequestParam("content") Address[] content){
        return content;
    }

簡單類型array

http://localhost:8080/test/direct/array?content=111,222,444

輸出

[
  "111",
  "222",
  "444"
]

對象array

http://localhost:8080/test/direct/array-obj?content=111,222

輸出

[
  {
    "name": "111",
    "district": null
  },
  {
    "name": "222",
    "district": null
  }
]

目前還不知道怎麼去填充其餘字段

接收list

/**
     * 須要post,requestBody
     * @param orders
     * @return
     */
    @RequestMapping(value = "/test/list",method = RequestMethod.GET)
    @ResponseBody
    public List<Order> getByList(List<Order> orders){
        return orders;
    }

    /**
     * http://localhost:8080/test/direct/list?content=111,222,444
     * http://localhost:8080/test/direct/list?content=[111,222,444]
     * 都不行
     * Failed to instantiate [java.util.List]: Specified class is an interface
     * @param content
     * @return
     */
    @RequestMapping(value = "/test/direct/list",method = RequestMethod.GET)
    @ResponseBody
    public List<String> getDirectList(List<String> content){
        return content;
    }

    /**
     * http://localhost:8080/test/direct/list2?content=111,222,444
     * @param content
     * @return
     */
    @RequestMapping(value = "/test/direct/list2",method = RequestMethod.GET)
    public List<String> getDirectList2(@RequestParam("content") List<String> content){
        return content;
    }

    /**
     * http://localhost:8080/test/direct/list3?content=111,222,444
     * @param content
     * @return
     */
    @RequestMapping(value = "/test/direct/list3",method = RequestMethod.GET)
    public List<Address> getDirectList3(@RequestParam("content") List<Address> content){
        return content;
    }

簡單類型list

http://localhost:8080/test/direct/list2?content=111,222,444

輸出

[
  "111",
  "222",
  "444"
]

須要@RequestParam("content") List<String> content 才能夠

對象list

http://localhost:8080/test/direct/list3?content=111,222,444

輸出

[
  {
    "name": "111",
    "district": null
  },
  {
    "name": "222",
    "district": null
  },
  {
    "name": "444",
    "district": null
  }
]

目前還不知道怎麼去填充其餘字段

RequestBody接收list

@RequestMapping(value = "saveUser", method = {RequestMethod.POST }})
    @ResponseBody 
    public void saveUser(@RequestBody List<User> users) {
         userService.batchSave(users);
    }

@RequestBody接收的是一個Json對象的字符串,而不是一個Json對象。然而在ajax請求每每傳的都是Json對象,後來發現用 JSON.stringify(data)的方式就能將對象變成字符串。同時ajax請求的時候也要指定dataType: "json",contentType:"application/json" 這樣就能夠輕易的將一個對象或者List傳到Java端,使用@RequestBody便可綁定對象或者List.

doc

相關文章
相關標籤/搜索