平時,post一些數據到springboot后台,都是習慣用在后台用一個對象來接參數,如:
前端:
login(ruleForm) {
var that = this;
that.$http
.post(
"/api/bookseller/m/account/login",
JSON.stringify({
account: ruleForm.userName,
pwd: ruleForm.userPwd,
}),
{headers: {
'Content-Type': 'application/json'
}}
)
.then(function (res) {
var loginInfo=res.data.data;
console.log(loginInfo)
that.$store.commit("Login",loginInfo);
that.$router.push("/Index");
})
.catch(function (error) {
console.log(error);
});
}
后端:
@PostMapping("/login")
public UserWithRoleModel login(@RequestBody @Validated LoginReqParams loginParams) {
return mAccountService.login(loginParams);
}
LoginReqParams 對象定義如下:
@Data public class LoginReqParams { @ApiModelProperty(value = "用戶名/手機號碼", required = true) @NotBlank(message = "用戶名/手機號碼不能為空") private String account; @ApiModelProperty(value = "密碼", required = true) @NotBlank(message = "密碼不能為空") private String pwd; }
但是,有時候,只是post一個字符串,如果定義一個對象,又覺得很浪費后台代碼,在此記錄一下如何psot一個參數給后台:
前端如下:
resetPwd() { var that = this; var resetIds = ""; for (var i = 0; i < that.multipleSelection.length; i++) { if (i > 0) { resetIds += ","; } resetIds += that.multipleSelection[i].id; } that.$http .post( "/api/bookseller/booksellerInfo/resetPwd", that.$qs.stringify({ids: resetIds }), ) .then(function(res) { that.$message({ type: "success", message: "重置成功!" }); console.log(res); });
}
重點代碼如下:
that.$qs.stringify({ids: resetIds })
后台代碼如下:
@RequestMapping(value = "resetPwd",method = RequestMethod.POST) public boolean resetPwd(@ApiParam("要更新的Id列表") @RequestParam("ids") String ids){ boolean b = bookSellerService.resetPwd(ids); if(!b){ throw new BooksellerException(ResultCodes.COMMON_SYSTEM_ERROR); } return b; }
