接收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简单类型

自动填充简单属性

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

自动填充多个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;
    }

请求

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

输出

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

另外一个bean的值

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

同名的属性,多个bean会被填充为同一个值,不同名的会自动去其他bean里头寻找

自动填充嵌套对象属性

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

输出

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

输出

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

输出

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


codecraft
11.9k 声望2k 粉丝

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很自豪告诉世人,我曾经将代码注入生命去打造互联网的浪潮之巅,那是个很疯狂的时代,我在一波波的浪潮上留下...


引用和评论

0 条评论