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
Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐