首页 > Web开发 > 详细

如何获取HttpServletResponse里面的内容

时间:2015-03-24 01:14:06      阅读:461      评论:0      收藏:0      [点我收藏+]

背景:在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

 

如何获取HttpServletResponse里面的内容

原文:http://www.cnblogs.com/dragonflyyi/p/4361497.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!