Content-Type有三种类型

// 1 默认的格式请求体中的数据会以json字符串的形式发送到后端
  'Content-Type: application/json '
// 2 请求体中的数据会以普通表单形式(键值对)发送到后端
  'Content-Type: application/x-www-form-urlencoded'
// 3 它会将请求体的数据处理为一条消息,以标签为单元,用分隔符分开。既可以上传键值对,也可以上传文件
  'Content-Type: multipart/form-data'
//4.纯文体的传输。空格转换为 “+” 加号,但不对特殊字符编码。
‘Content-Type':text/plain

我们可以这样配置请求头

axios({
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    },
    method: 'post',
    url: url,
    data: Qs.stringify(params)
})

或者是写到post的js文件

axios.js

axios.defaults.headers.post['Content-Type'] = "application/x-www-form-urlencoded"

对于不同的请求头,后端接口要用不同的注解吸收数据,否则会报404

multipart/form-data指定传输数据为二进制类型,比如图片、mp3、文件。

text/plain:纯文体的传输。空格转换为 “+” 加号,但不对特殊字符编码。

@RequestParam

1.均支持POST,GET请求
2.只支持Content-Type: 为 application/x-www-form-urlencoded编码的内容。Http协议中,如果不指定Content-Type,则默认传递的参数就是application/x-www-form-urlencoded类型)

@PostMapping
    public Result shopBtn(@RequestParam String token){
        System.out.println(token);
          return userShopService.checkShop(token);
    }

@RequestBody绑定一个对象实体

1.不支持get请求,因为get请求没有HttpEntity
2.必须要在请求头中申明content-Type(如application/json).springMvc通过HandlerAdapter配置的HttpMessageConverters解析httpEntity的数据,并绑定到相应的bean上
3.只能一个@RequestBody。
4.可以与@RequestParam一起使用,但建议最好不要与@RequestParam一起使用,是因为@RequestBody会将InputStream吃掉,造成后面的@RequsetParam无法匹配到参数而报400
 

 @PostMapping("/shopLogin")
    public Result shopLogin(@RequestBody User user){
        return userService.shopLogin(user);
    }
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。