一个请求可以分为请求头和请求体,正常的get请求,请求参数是放在请求头中,比如:
https://cn.bing.com/search?q=get 问号后的q是请求的key,等号后边的get是请求value。

一.data类型传参

而post请求一般把请求参数放在请求体中,使用axios发送post请求正常使用方式为

axios.post('/user', {firstName: 'Fred',lastName: 'Flintstone'}).then(()=>{})

这种对应的其实是axios的data类型

this.$axios({
   url: '/user/login' ,
   method: 'post',
   headers: {
      'Content-Type': 'application/json'
   },
   data:{
      username: this.user,
      pwd: this.pwd
   }
}).then(() => {
})

此种方式,后端需要使用@RequestBody +实体类来接收。

二.params类型传参

上述的data可以换成params类型传参

this.$axios({
   url: '/user/login' ,
   method: 'post',
   headers: {
      'Content-Type': 'application/json'
   },
   params:{ //注意这里
      username: this.user,
      pwd: this.pwd
   }
}).then(() => {
})

这种方式其实和get请求类似,把请求参数放到了请求头中,
http://127.0.0.1/user/login?username=admin&pwd=1234
所以这种需要使用@RequestParam来接收参数

三.@RequestBody和@RequestParam

@RequestParam:

一般用来处理请求头中的参数,用来处理Content-Type= application/x-www-form-urlencoded编码的内容

@RequestBody:

一般用来处理请求体中的参数,Content-Type不等于 application/x-www-form-urlencoded
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐