“status”:415
“error”:”Unsupported Media Type”
“exception”:”org.springframework.web.HttpMediaTypeNotSupportedException”
原因:没有使用JSON.stringify(rowData)方法将请求参数转化为json字符串
报(Unsupported Media Type)异常主要原因为,前端工具请求接口的类型和后端服务器定义的类型不一致造成,
一般工具请求类型和后端保持一致后,即可正常请求。
原因:根据接口所传参数的不同,post请求的Content-type有四种:
application/x-www-form-urlencoded(默认)
application/xml
application/json
multipart/form-data
我所对接的接口所传的参数是json格式的,代码中没有设置请求头header的Content-Type,默认是application/x-www-form-urlencoded。
URIBuilder uriBuilder = new URIBuilder(url); //创建一个http post请求 HttpPost httpPost = new HttpPost(uriBuilder.build()); //设置请求头header httpPost.setHeader("Content-Type","application/json"); //设置请求数据 httpPost.setEntity(new StringEntity(strJson,"utf-8"));