HttpServletResponseWrapper 是什麼?
ServletResponse 的包裝類,相關設計模式 裝飾者模式。
運行環境
jdk 1.7
spring boot 整合的web環境
關鍵代碼
Wrapper
package com.basic.m.web.config; import javax.servlet.ServletOutputStream; import javax.servlet.WriteListener; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponseWrapper; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintWriter; public class CustomizeResponseWrapper extends HttpServletResponseWrapper { private ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); private PrintWriter printWriter = new PrintWriter(outputStream); public CustomizeResponseWrapper(HttpServletResponse response) { super(response); } @Override public PrintWriter getWriter() throws IOException { return printWriter; } @Override public ServletOutputStream getOutputStream() throws IOException { return new ServletOutputStream() { @Override public boolean isReady() { return false; } @Override public void setWriteListener(WriteListener listener) { } @Override public void write(int b) throws IOException { outputStream.write(b); } }; } public void flush(){ try { printWriter.flush(); printWriter.close(); outputStream.flush(); outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } public ByteArrayOutputStream getByteArrayOutputStream(){ return outputStream; } public String getTextContent() { flush(); return outputStream.toString(); } }
Filter
1 package com.basic.m.web.config; 2 3 import com.alibaba.fastjson.JSONObject; 4 import org.slf4j.Logger; 5 import org.slf4j.LoggerFactory; 6 import org.springframework.core.annotation.Order; 7 8 import javax.servlet.*; 9 import javax.servlet.annotation.WebFilter; 10 import javax.servlet.http.HttpServletResponse; 11 import java.io.IOException; 12 13 @Order(2) 14 @WebFilter(urlPatterns = "/*", filterName = "responseFilter") 15 public class ResponseFilter implements Filter { 16 private static final Logger logger = LoggerFactory.getLogger(ResponseFilter.class); 17 18 @Override 19 public void init(FilterConfig filterConfig) throws ServletException { 20 logger.info("enter ResponseFilter init method"); 21 } 22 23 @Override 24 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 25 logger.info("enter ResponseFilter doFilter method"); 26 CustomizeResponseWrapper responseWrapper = new CustomizeResponseWrapper((HttpServletResponse) response); 27 chain.doFilter(request, responseWrapper); 28 29 System.out.println(responseWrapper.getTextContent()); 30 31 /* MyResponseWrapper responseWrapper = new MyResponseWrapper((HttpServletResponse) response); 32 chain.doFilter(request, responseWrapper);*/ 33 34 System.out.println(responseWrapper.getTextContent()); 35 36 String content = responseWrapper.getTextContent(); 37 JSONObject resultJson = new JSONObject(); 38 resultJson.put("code", "ok"); 39 resultJson.put("msg", "success"); 40 resultJson.put("result", content); 41 42 response.setContentLength(resultJson.toJSONString().length()); 43 response.setContentType("application/json;charset=utf-8"); 44 ((HttpServletResponse) response).setStatus(203); 45 response.getOutputStream().write(resultJson.toJSONString().getBytes()); 46 } 47 48 @Override 49 public void destroy() { 50 51 } 52 }
參考java
經過百度和google 零零散散的查詢相關代碼後本身實現了一個,這裏須要特別注意的是網絡上搜索出來的大部分結果都是經過重寫 getWriter() 方法來寫入流。這裏我一直搞不明白爲何。由於我的調試時用於都只會進入 getOutputStream() 方法來寫入流。web