傳參的幾種形式
@RequestParam(value="userName") String userName
單個屬性,適用於基本類型及其包裝類型和String
的傳遞@RequestParam Map<String, Object> mapParams
通用型方式,通過 JDK 內置對象 Map,接收所有鍵值對的參數@RequestBody User user
將整個 json 請求體包裹起來,作爲 raw 傳值;Content-Type: application/json@ModelAttribute User user
將所有鍵值對映射到自定義對象的屬性上,有點類似於@RequestParam Map
(注意對象爲空和對象的所有屬性爲空的區分)- 支持複雜參數類型以及多參數的傳遞,如: rest-api 接口定義:
@GetMapping(value = "/rpc/erp/itemGift/page")
ItemMapResponse giftList(@RequestParam("searchWord") String searchWord, @RequestParam("restPageJson") String restPageJson, @RequestBody ItemSupplierSearchRequest itemSupplierSearchRequest);
接口實現類:
@Override
public ItemMapResponse giftList(String searchWord, String restPageJson, @RequestBody ItemSupplierSearchRequest itemSupplierSearchRequest) {
logger.info("查詢贈品商品列表入參:restPageJsonc={},searchWord={},restItemSearchDTO={}", restPageJson, searchWord,
JSONObject.toJSONString(itemSupplierSearchRequest));
...
}
用 cURL 請求(或者用 Charles 抓包拷貝 cURL 地址):
curl --location --request GET 'http://localhost:9012/rpc/erp/itemGift/page?restPageJson=%7B%22pageNo%22:1,%22pageSize%22:10%7D&searchWord=%E8%B5%A0%E5%93%81' \
--header 'Content-Type: application/json' \
--data-raw '{"title":"測試"}'
解決 @RequestBody 不能繼承的問題
- https://snippets.cacher.io/snippet/de950fbc3c9169c3e92f
- 框架層面改動大,建議所有實現類 resouce 層還是全部將參數註解覆蓋一遍接口中的定義。