咕噜咕噜噜^
2021-04-24 01:08
采纳率: 100%
浏览 306
已采纳

SpringBoot获取vue端的json不为空但 RequestBody的实体为空???

Login.vue

<template>
  <div>
    手机号:<input type="text" v-model="loginForm.userphone" placeholder="请输入手机号"/>
    <br><br>
    密码: <input type="password" v-model="loginForm.userpass" placeholder="请输入密码"/>
    <br><br>
    <button v-on:click="login">登录</button>
  </div>
</template>

<script>

export default {
  name: 'Login',
  data () {
    return {
      loginForm: {
        userphone: '',
        userpass: ''
      },
      responseResult: []
    }
  },
  methods: {
    login () {
      this.$axios
        .post('/login', {
          userphone: this.loginForm.userphone,
          userpass: this.loginForm.userpass
        })
        .then(successResponse => {
          if (successResponse.data.code === 200) {
            this.$router.replace({path: '/index'})
          }
        })
        .catch(failResponse => {
        })
    }
  }
}
</script>

RequestLoginVo.java

@Getter
@Setter
public class RequestLoginVo {
    @JsonProperty(value = "userPhone")
    private  String userPhone;
    @JsonProperty(value = "userPass")
    private String userPass;

//    private String captcha;
}

UserController.java

    @CrossOrigin
    @PostMapping(value = "api/login")
    @ResponseBody
    public Result login(@RequestBody RequestLoginVo requestLoginVo){

        System.out.println(requestLoginVo.getUserPhone());
        System.out.println(requestLoginVo.getUserPass());
        System.out.println(requestLoginVo);

        User user=userService.queryByUserPhone(requestLoginVo.getUserPhone());
        //用户不存在或密码错误
        if(user==null || !user.getUserPass().equals(Md5Utils.hash(requestLoginVo.getUserPass()))){
            if(user==null)
                System.out.println("user==null");
            if(!user.getUserPass().equals(Md5Utils.hash(requestLoginVo.getUserPass())))
                System.out.println("wrong pass");
            return new Result(400,"失败");
        }
        Map<String,Object> userMap=new HashMap<>();
        userMap.put("userId",user.getUserId());
        if(user.getUserType()== Helpless.UserTypeEnum.ORDUSER){
            userMap.put("isAdmin",true);
            return new Result(200,"成功");
//            return Result.getSuccess().setData(userMap);
        }else{
            userMap.put("isAdmin",false);
            return new Result(200,"成功");
//            return Result.getSuccess().setData(userMap);
        }
    }

把RequestLoginVo换成String类型,能输出正确的json格式,RequestLoginVo类型就为null

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

5条回答 默认 最新

  • CSDN专家-Time 2021-04-24 07:10
    最佳回答

    axios默认带的json格式,你回传的数据有问题。这个全是小写,

    这个驼峰命名,映射不到了。

    所以应该把前端 userphone改成 userPhone,userpass改成userPass

    评论
    解决 无用
    打赏 举报
查看更多回答(4条)

相关推荐 更多相似问题