zoukankan      html  css  js  c++  java
  • 获取text/plain格式http请求中的数据

    转载: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 "";
    }
    
  • 相关阅读:
    第63天python学习异常
    第62天python 学习TCP三次握手四次挥手详解
    文件操作
    内置函数
    函数递归
    函数补充
    函数
    购物车程序作业
    集合内置函数
    字典三级菜单
  • 原文地址:https://www.cnblogs.com/wanthune/p/11101179.html
Copyright © 2011-2022 走看看