2017-10-10 38 views
0

我想用新的過濾器替換現有的json響應(在某些情況下)。我想要做的是從過濾器中讀取現有響應(JSON)。用新值修改它並回寫回應。但結果顯示了這兩個答案。在春天使用新的servlet過濾器替換現有的響應mvc

也就是說,我從響應中讀到了什麼,以及我最近添加了什麼。但我需要用新的替換舊的響應。下面添加代碼。

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 
{ 
    try{ 
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     final PrintStream ps = new PrintStream(baos); 

     chain.doFilter(request,new HttpServletResponseWrapper((HttpServletResponse)response) { 
       @Override 
      public ServletOutputStream getOutputStream() throws IOException { 
       return new DelegatingServletOutputStream(new TeeOutputStream(super.getOutputStream(), ps) 
       ); 
      } 
      @Override 
      public PrintWriter getWriter() throws IOException { 
       return new PrintWriter(new DelegatingServletOutputStream (new TeeOutputStream(super.getOutputStream(), ps)) 
       ); 
      } 
      }); 
     /* get existing response as string*/ 
     String respopn=baos.toString(); 
     JSONObject json=new JSONObject(respopn); 
     JSONObject dMap=new JSONObject(json.get("dataMap")); 
     dMap.put("new", "newValue"); 
     json.put("dataMap", dMap); // Modified the old datamap with new json 
     JsonMapper jsonMap=new JsonMapper(); 
     jsonMap.setJson(json); 
     String str=jsonMap.getJson(); 
     byte[] responseToSend = restResponseBytes(jsonMap); 
     response.getOutputStream().write(responseToSend); // write to response only the new one 



    } 
    catch(Exception e) 
    { 
     e.printStackTrace(); 
    } 
    } 


    private byte[] restResponseBytes(Object response) throws IOException { 
     String serialized = new ObjectMapper().writeValueAsString(response); 
     return serialized.getBytes(); 
    } 
+0

可能重複:https://開頭stackoverflow.com/questions/14736328/looking-for-an-example-for-inserting-content-into-the-response-using-a-servlet-f,https://stackoverflow.com/questions/34439260/replace- response-body-using-filter,https:// stackoverf low.com/questions/24452318/how-to-alter-the-body-of-a-http-response-in-a-filter – 2017-10-10 06:03:26

回答

1

我覺得下面的代碼片段應該工作。上面的問題是它正在追加到現有的數據而不是重寫。我們需要創建一個數據存儲位置的副本,並在操作原始ServletResponse後複製。希望下面的代碼片段解決您的問題。

下面

是主要做過濾方法 -

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 

    ServletResponseWrapperCopier capturingResponseWrapper = new ServletResponseWrapperCopier(
       (HttpServletResponse) response); 

    chain.doFilter(request, capturingResponseWrapper); 
    try{ 
     String respopn = capturingResponseWrapper.getCaptureAsString();  
     JSONObject json=new JSONObject(respopn); 
     JSONObject dMap=new JSONObject(json.get("dataMap")); 
     dMap.put("new", "newValue"); 
     json.put("dataMap", dMap); // Modified the old datamap with new json 
     JsonMapper jsonMap=new JsonMapper(); 
     jsonMap.setJson(json); 
     String str=jsonMap.getJson(); 
     response.getOutputStream().write(str.getBytes()); 
    } catch(Exception e){ 
     log.error(""); 
    } 
} 

下面類用於將ServletResponse的複製在上面的代碼段

public class ServletResponseWrapperCopier extends HttpServletResponseWrapper{ 

private final ByteArrayOutputStream capture; 
private ServletOutputStream output; 
private PrintWriter writer; 

public ServletResponseWrapperCopier(HttpServletResponse response) { 
    super(response); 
    capture = new ByteArrayOutputStream(response.getBufferSize()); 
} 

@Override 
public ServletOutputStream getOutputStream() { 
    if (writer != null) { 
     throw new IllegalStateException(
       "getWriter() has already been called on this response."); 
    } 

    if (output == null) { 
     output = new ServletOutputStream() { 
      @Override 
      public void write(int b) throws IOException { 
       capture.write(b); 
      } 

      @Override 
      public void flush() throws IOException { 
       capture.flush(); 
      } 

      @Override 
      public void close() throws IOException { 
       capture.close(); 
      } 
     }; 
    } 

    return output; 
} 

@Override 
public PrintWriter getWriter() throws IOException { 
    if (output != null) { 
     throw new IllegalStateException(
       "getOutputStream() has already been called on this response."); 
    } 

    if (writer == null) { 
     writer = new PrintWriter(new OutputStreamWriter(capture, 
       getCharacterEncoding())); 
    } 

    return writer; 
} 

public byte[] getCaptureAsBytes() throws IOException { 
    if (writer != null) { 
     writer.close(); 
    } else if (output != null) { 
     output.close(); 
    } 

    return capture.toByteArray(); 
} 

public String getCaptureAsString() throws IOException { 
    return new String(getCaptureAsBytes(), getCharacterEncoding()); 
} 

}