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;
   }

 

 
posted @ 2021-03-13 16:56  一夜听春雨  阅读(936)  评论(0编辑  收藏  举报