从request中获取json数据

  /**
     * 从request中获取json数据
     *
     * @param request
     * @return
     */
    public static String getJsonRequest(HttpServletRequest request) {
        StringBuilder sb = new StringBuilder();
        String line = null;
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            String string = sb.toString();
            return string;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                request.getInputStream().close();
            } catch (IOException e) {
                return null;
            }
        }
    }
//获取
String req = getJsonRequest(request);
log.info(req);
JSONObject json = JSONObject.parseObject(req);

String name=json.getString("name");

发布了24 篇原创文章 · 获赞 0 · 访问量 728

猜你喜欢

转载自blog.csdn.net/weixin_43355449/article/details/105136649