spring boot报Unsupported Media Type Content type '*/*;charset=UTF-8' not supported
1.请求设置Content-Type:application/json即可
ajax一般默认:Content-Type: application
/x-www-form-urlencoded
;charset=utf-8
2.服务端:
controller
package com.example.demo; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class helloController { @RequestMapping(value="/hello") public String say(){ String str="my spring boot web project hihihi"; System.out.println(str); return str; } @RequestMapping(value="/test") public String testObj(@RequestBody User user){ System.out.println(user.toString()); return user.toString(); } }
实体user
package com.example.demo; public class User { private String userName; private String pwd; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public String toString(){ return userName+""+pwd; } }
3.输出
postman中测试json接口
1、接口链接如下:https://ddd.com/api/contract/core/order/open
2、参数如下:
{
“assureAsset”: “EXT”,
“assureQuantity”: 0,
“baseAsset”: “BTC”,
“buyQuantity”: 0.005,
“buySide”: “long”,
“multiple”: 50.0,
“openPrice”: 0,
“quoteAsset”: “USDT”,
“symbol”: “BTC_USDT”
}
3、在postman里面参数添加到body里面,如下图:
4、请求头里面的参数要除了认证和客户端之外,一定要包含:content-type,如下图所示:
区别于直接在params填写参数的种类,在之前的文章里我们提到过,如果是一般的接口,那么在headers里面要包括如下参数: