vue post參數到springboot后台


平時,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;
   }

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM