如何获取HttpServletResponse里面的内容

背景:在SPRING 框架之中, 有一个服务端需要提供多种形态的服务,这里的多种形态只是返回值得展示形式(其实 数据内在逻辑完全一样), 比如:

         形式1:   JSONP({“key1”: value1, "key2":value2, "key3":value3, ....})

         形式2:  {“key1”: value1, "key2":value2, "key3":value3, ....}

 

为了使得后台业务处理代码一样(不做任何区分),现在理由过滤器,对返回接口进行处理,根据需要加上 :JSONP(...) 

 

主要实现步骤如下:

 

1.  在web.xml 中配置过滤器

<filter>
<filter-name>RewriteResponse</filter-name>
<filter-class>com.robin.filter.RewriteResponse</filter-class>
</filter>
 
<filter-mapping>
<filter-name>RewriteResponse</filter-name>
<servlet-name>/*</servlet-name>
</filter-mapping>
 //  斜杠星代表匹配任何请求

 

2.   重点在于RewriteResponse  过滤器


public class RewriteResponseFilter extends Filter {

public String description() {
// TODO Auto-generated method stub
return null;
}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException {
// TODO Auto-generated method stub

ResponseWrapper responseWrapper = new ResponseWrapper((HttpServletResponse) response);

try {
chain.doFilter(request, responseWrapper);
} catch (ServletException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

String responseContent = new String(responseWrapper.getDataStream());

RestResponse fullResponse = new RestResponse(205, "OK-MESSAGE",responseContent);

byte[] responseToSend = restResponseBytes(fullResponse);

response.getOutputStream().write(responseToSend);

}


  private byte[] restResponseBytes(RestResponse response) throws IOException {
    String serialized = new ObjectMapper().writeValueAsString(response);
    return serialized.getBytes("UTF-8");
  }

}

 

3.    通用业务类(用于返回)

public class RestResponse implements Serializable  {
private int status;

private String message;

private Object data;

public RestResponse(int status, String message, Object data) {
this.status = status;
this.message = message;
this.data = data;
}

public int getStatus() {
return status;
}

public void setStatus(int status) {
this.status = status;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public Object getData() {
return data;
}

public void setData(Object data) {
this.data = data;
}


}

 

 

4.  实现ServletOutputStream  指定的扩展

public class FilterServletOutputStream extends ServletOutputStream {

DataOutputStream output;
public FilterServletOutputStream(OutputStream output) {
    this.output = new DataOutputStream(output);
}

@Override
public void write(int arg0) throws IOException {
    output.write(arg0);
}

@Override
public void write(byte[] arg0, int arg1, int arg2) throws IOException {
    output.write(arg0, arg1, arg2);
}

@Override
public void write(byte[] arg0) throws IOException {
    output.write(arg0);
}
}

 

5.  替换原来的输出

public class ResponseWrapper extends HttpServletResponseWrapper {

ByteArrayOutputStream output;
FilterServletOutputStream filterOutput;
HttpResponseStatus status = HttpResponseStatus.OK;

public ResponseWrapper(HttpServletResponse response) {
    super(response);
    output = new ByteArrayOutputStream();
}

@Override
public ServletOutputStream getOutputStream() throws IOException {
    if (filterOutput == null) {
        filterOutput = new FilterServletOutputStream(output);
    }
    return filterOutput;
}

public byte[] getDataStream() {
    return output.toByteArray();
}
}

 

文章应用:  http://stackoverflow.com/questions/25020331/spring-mvc-how-to-modify-json-response-sent-from-controller

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。