package com.bj.common; import com.bj.utils.JsonUtils; import com.bj.utils.SysUtils; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.OutputStream; import java.util.HashMap; import java.util.Map; public class ResponseTest { /** *response 给HttpServletResponse中设置返回值 * @param request * @param response * @throws IOException */ public void setResponseBusData(HttpServletRequest request, HttpServletResponse response) throws IOException{ Map<String,Object> busData=new HashMap<>(); busData.put("code","T001"); busData.put("name","张三"); OutputStream outputStream=response.getOutputStream(); //获取response的输出流 try { String jsonBusData = JsonUtils.mapToJson(busData);//将业务数据转换为json //将字符转换成字节数组,指定以UTF-8编码进行转换,以解决中文乱码问题 byte[] busDataByte=jsonBusData.getBytes("UTF-8"); outputStream.write(busDataByte); }catch (Exception e){ e.fillInStackTrace(); }finally { if(SysUtils.isNotNullOrEmpty(outputStream)){ outputStream.close(); } } } }
HttpServletResponse中设置返回数据
于 2024-09-12 14:50:33 首次发布