request中获取post的json对象数据content-type=“text/plain”

request中获取post的json对象数据content-type=“text/plain”

public ReqReturnEntity getPostByTextPlain(HttpServletRequest request) throws IOException {
ReqReturnEntity result = new ReqReturnEntity();

List<CEntity> cEntityList = new ArrayList<>();
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);
    }
    logger.info(contentBuffer.toString());

    String reqInfo = JSONUtil.formatJsonStr(contentBuffer.toString());//text文本转json
    JSONArray jsonArray = JSONUtil.parseArray(reqInfo);//json转jsonArray
    cEntityList = (List<CEntity>) jsonArray.toList(CEntity.class);//jsonArray转List集合
    result = SaveSteelArrivalPlan(cEntityList);
} catch (IOException e) {
    e.printStackTrace();
    logger.error("[获取request中用POST方式“Content-type”是“text/plain”发送的json数据]异常:{}", e.getCause());
}
return result;

}

com.alibaba fastjson 1.2.28

版权声明:本文为weixin_54613809原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。