如何获取HttpServletResponse里面的内容
1个回答
展开全部
背景:在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();
}
}
形式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();
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询