response的返回方法

常用的返回类方法

// 返回 json List
public void returnJsonList(BatchRespBean batchRespBean) {
		PrintWriter out = null;
		try {
			response.setContentType("application/json;charset=UTF-8");
			response.setHeader("Cache-Control", "no-cache");
			out = response.getWriter();
			out.print(JSON.toJSONString(batchRespBean));
		} catch (Exception e) {
			log.error("批量处理接口返回异常", e);
		} finally {
			if (out != null) {
				out.flush();
				out.close();
			}
		}
	}
// 返回json ,就是参数不一样
	public void returnJson(RespBean respBean) {
		PrintWriter out = null;
		try {
			response.setContentType("application/json;charset=UTF-8");
			response.setHeader("Cache-Control", "no-cache");
			out = response.getWriter();
			out.print(JSON.toJSONString(respBean));
		} catch (Exception e) {
			log.error("接口返回异常", e);
		} finally {
			if (out != null) {
				out.flush();
				out.close();
			}
		}
	}
// 返回 String
	public void returnStr(String str) {

		PrintWriter out = null;
		try {
			response.setCharacterEncoding("UTF-8");
			response.setContentType("text/html; charset=UTF-8");
			response.setHeader("Cache-Control", "no-cache");
			out = response.getWriter();
			out.print(str);
		} catch (IOException e) {
			log.error("调用 returnStr方法出错!");
		} finally {
			if (out != null) {
				out.flush();
				out.close();
			}
		}

	}

你可能感兴趣的:(工具类,java)