欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

通过HttpServletRequest转换获得json对象

程序员文章站 2022-03-10 13:04:43
...
private JSONObject getRequestPostJson(String msisdn,
			HttpServletRequest request) {
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(
					request.getInputStream()));
			String line = null;
			StringBuilder sb = new StringBuilder();
			while ((line = br.readLine()) != null) {
				sb.append(line);
			}

			String reqBody = URLDecoder.decode(sb.toString(), HTTP.UTF_8);
			logger.info("Request Body:" + reqBody);
			JSONObject json = new JSONObject(reqBody);
			logger.info("[getRequestPostJson][" + msisdn
					+ "]-- get request body with json success.");
			return json;
		} catch (Exception e) {
			logger.error("[getRequestPostJson][" + msisdn
					+ "]-- get request body with json fail.Exception message:" + e.getMessage());
			return null;
		}

	}