SpringMVC data binding instance

  spring-mvc

Receive 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;
    }

Without additional comments, bean properties can be automatically populated, where beans support nested objects, nested list objects, nested list simple types, nested array simple types

Automatically populate simple attributes

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

Automatically populate multiple bean

Code

    @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;
    }

Request

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

Output

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

The value of another bean

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

For attributes with the same name, multiple beans will be filled with the same value, and those with different names will be automatically searched in other beans.

Automatically populate nested object properties

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

Output

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

Automatically populate simple type array

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

Output

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

Automatically populate simple type list

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

Output

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

Automatically populate array objects

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

Output

{
  "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
    }
  ]
}

Automatically populate list objects

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

Output

{
  "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
}

Receive 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;
    }

Simple type array

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

Output

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

Object array

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

Output

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

At present, I don’t know how to fill other fields.

Receive 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;
    }

Simple type list

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

Output

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

The @ requestparam (“content”) list < string > content is required.

Object list

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

Output

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

At present, I don’t know how to fill other fields.

RequestBody receive list

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

@RequestBody received a string of Json objects instead of a Json object. However, in ajax requests, Json objects are often passed. Later, it was discovered that JSON.stringify(data) can transform objects into strings. At the same time, when ajax requests are made, datatype: “JSON” and contenttype: “application/JSON” should also be specified, so that an object or List can be easily passed to Java side, and the object or List can be bound using @RequestBody.

doc