轉載:https://blog.csdn.net/Jin19950615/article/details/85062215
springboot項目,在接收text/plain格式的時候,無法通過@requestBody得到請求中的json信息,需要對請求中的參數進行解析。
@requestBody注解常用來處理content-type不是默認的application/x-www-form-urlcoded編碼的內容,比如說:application/json或者是application/xml等。一般情況下來說常用其來處理application/json類型。###
異常 type 'text/plain;charset=UTF-8' not supported。
/**
* 解析text/plain格式請求中的json
*
* @param request
* @return
*/
public static String fetchPostByTextPlain(HttpServletRequest request) {
try {
BufferedReader reader = request.getReader();
char[] buf = new char[512];
int len = 0;
StringBuffer contentBuffer = new StringBuffer();
while ((len = reader.read(buf)) != -1) {
contentBuffer.append(buf, 0, len);
}
return contentBuffer.toString();
} catch (IOException e) {
e.printStackTrace();
log.error("[獲取request中用POST方式“Content-type”是“text/plain”發送的json數據]異常:{}", e.getCause());
}
return "";
}
