Spring MVC 使用 HttpServletResponseWrapper 修改返回结果
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 }
参考
通过百度和google 零零散散的查询相关代码后自己实现了一个,这里需要特别注意的是网络上搜索出来的大部分结果都是通过重写 getWriter() 方法来写入流。这里我一直搞不明白为什么。因为个人调试时用于都只会进入 getOutputStream() 方法来写入流。