当前位置: 代码迷 >> 综合 >> 通过response 响应数据 json 格式
  详细解决方案

通过response 响应数据 json 格式

热度:78   发布时间:2024-01-31 14:13:57.0

 

/***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();  }  }}

 

  相关解决方案