controller 接收Post傳遞數據
https://blog.csdn.net/weixin_42784951/article/details/106639657
1 傳遞字符串、基本類型
1、直接在方法形參中接收參數
直接把表單的參數寫在Controller相應的方法的形參中,適用於get方式提交,不適用於post方式提交。若"Content-Type"="application/x-www-form-urlencoded"
,可用post提交
url形式:http://localhost:8080/SSMDemo/demo/addUser1?username=lixiaoxi&password=111111
提交的參數需要和Controller方法中的入參名稱一致。這種只可以將數據填在form
表單中,使用Json字符串提交的時候會默認為null。
2、使用 @RequestBody String
形式接收:
@RequestBody 注解常用來處理 content-type 是 application/json 編碼的內容,而不能用來處理 application/x-www-form-urlcoded 編碼的內容。因此@RequestBody的時候應該使用Json形式傳遞:
{
"name" : "天天"
}
- 1
- 2
- 3
@RequestBody
接收的是一個Json
對象的字符串,而不是一個Json對象。我們可以通過如下實例看到:
@PostMapping("/test")
public Response TestBlackList(@RequestBody String blackRooms) {
System.out.println(blackRooms.getClass());
System.out.println(blackRooms);
return Response.success();
}
- 1
- 2
- 3
- 4
- 5
- 6
后台輸出:
class java.lang.String
{
"blackRooms" : "1247,4545,5211"
}
- 1
- 2
- 3
- 4
這時我們要拿到對應的數據,就必須對所得到的字符串進行解析。
3、使用 Map<String, Object>接收。Spring MVC會自動將傳遞的JSON字符串封裝到map類型中,依據官網說明,我們需要創建自己的Entity接收參數。
@PostMapping("/saveBlackRooms")
public Response saveBlackList(@RequestBody Map<String, String> blackRooms) {
String blackRoomList = blackRooms.get("blackRoomList");
if (validateBlackList(blackRoomList)) {
PkMatchGlobalConfig pkMatchGlobalConfig = new PkMatchGlobalConfig();
pkMatchGlobalConfig.setBlackRoomList(blackRoomList);
return blackListService.saveGlobalConfig(pkMatchGlobalConfig);
}
return Response.error(-1, "請輸入正確的黑名單房間格式");
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
2 傳遞 列表
前端傳遞列表數據需要請求的時候在前端指定dataType: “json”,contentType:“application/json”,在后台使用 @RequestBody注解可以直接解析成為相對應的列表或者實體類。
1、前端通過 Json
傳遞實體類 Config
列表:
[
{"configKey": "blackRoomList","configValue": "1234"},
{"configKey": "blackRoomList","configValue": "1234"},
{"configKey": "blackRoomList","configValue": "1234"},
{"configKey": "blackRoomList","configValue": "1234"}
]
- 1
- 2
- 3
- 4
- 5
- 6
2、傳遞的實體類中包含列表, 如下面實體類包含 List<App_passenger>
列表:
public class App_order implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
/**
*聯系人姓名
*/
@TableField("buyer_name")
private String buyer_name;
/**
* 實付電話
*/
@TableField("buyer_mobile")
private String buyer_mobile;
/**
* 聯系人郵箱
*/
@TableField("buyer_email")
private String buyer_email;
@TableField(exist=false)
private List<App_passenger> passengers;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
提交格式:
{
"buyer_email": "1365992340@qq.com",
"buyer_name": "lzs",
"buyer_mobile": "123456789",
"passengers": [{
"create_time": "2019-04-24 14:56:30",
"passport": "b1234888",
"user_id": 1,
"mobile": "17888888888",
"name": "李xx",
"id": 1,
"identity_card": "370403xxxx8888"
}, {
"create_time": "2019-04-24 14:56:30",
"passport": "b1234222",
"user_id": 1,
"mobile": "17222222222",
"name": "徐xx",
"id": 3,
"identity_card": "370403xxxx3333"
}, {
"create_time": "2019-04-24 14:56:30",
"passport": "b1234111",
"user_id": 1,
"mobile": "17111111111",
"name": "張xx",
"id": 2,
"identity_card": "370403xxxx2222"
}]
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
3 接受實體類
前端傳遞 實體類 Config
,與上面類似,使用@RequestBody注解解析成為相對應的實體類。
{
"id": 0,
"name": 1,
"sex": 64,
"mothor": 62,
"father": 62
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
參考文章