/** *json 格式样例 * * @param response * @param jsonString */ public void responseJsonString(HttpServletResponse response, String jsonString) { response.setContentType("application/json; charset=utf-8"); byte[] bytes; try { bytes = jsonString.getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } response.setContentLength(bytes.length); try { OutputStream os = response.getOutputStream(); os.write(bytes); os.flush(); if (logger.isDebugEnabled()) { logger.debug("Send the client " + jsonString); } } catch (IOException e) { logger.error("Exception happens.", e); throw new RuntimeException(e); }finally { if (os!= null) { os.close(); } } }
request通过流获取参数
piblic void getJson(HttpServletRequest request){ BufferedReader streamReader = new BufferedReader( new InputStreamReader(request.getInputStream(), "UTF-8")); StringBuilder responseStrBuilder = new StringBuilder(); String inputStr; while ((inputStr = streamReader.readLine()) != null){ responseStrBuilder.append(inputStr); } JSONObject jsonObject = JSONObject.parseObject(responseStrBuilder.toString()); }
通过response 响应数据 json 格式
于 2020-07-24 09:29:54 首次发布