修改springmvc返回ajax方式的json資料

來源:互聯網
上載者:User

標籤:spring   攔截   

現在由這麼一個需求,就是修改幾個功能模組的springmvc的ajax請求傳回值(對傳回值加密),

因為controller很多,而且以前抱著開閉原則,這裡就選擇使用攔截器的方式(是Filter不是spring的handlerFilter)。廢話不多少,上代碼。(原始controller裡返回的都是json資料)。


  1. 修改web.xml 添加filter

<filter>

<filter-name>jsonFilter</filter-name>

<filter-class>com.spr.util.UpdateJsonFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>jsonFilter</filter-name>

<url-pattern>/android/*</url-pattern><!-- 只攔截這個請求路徑下的action-->

</filter-mapping>

2.處理類

package com.spr.util;


import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.PrintWriter;


import javax.servlet.Filter;

import javax.servlet.FilterChain;

import javax.servlet.FilterConfig;

import javax.servlet.ServletException;

import javax.servlet.ServletOutputStream;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpServletResponseWrapper;


import net.sf.json.JSONObject;


import org.apache.commons.lang3.StringUtils;

import org.springframework.http.HttpStatus;


public class UpdateJsonFilter implements Filter {


public void doFilter(ServletRequest request, ServletResponse response,

FilterChain chain) throws IOException, ServletException {

ByteResponseWrapper brw = new ByteResponseWrapper(

(HttpServletResponse) response);

chain.doFilter(request, brw);

String out = new String(brw.getBytes());

if (!StringUtils.isBlank(out)) {

            //處理加密的業務(略過)

out = out.toString();

} else {

out = "";

}

response.setContentType("application/json; charset=utf-8");

response.setContentLength(out.getBytes().length);

response.getOutputStream().write(out.getBytes());

}


public void destroy() {

}


public void init(FilterConfig filterConfig) {

}


static class ByteResponseWrapper extends HttpServletResponseWrapper {

private PrintWriter writer;

private ByteOutputStream output;

private int code = 200;


public byte[] getBytes() {

writer.flush();

return output.getBytes();

}


public ByteResponseWrapper(HttpServletResponse response) {

super(response);

output = new ByteOutputStream();

writer = new PrintWriter(output);

}


@Override

public PrintWriter getWriter() {

return writer;

}


@Override

public ServletOutputStream getOutputStream() throws IOException {

return output;

}


public int getStatus() {

return code;

}


public void sendRedirect(String location) throws IOException {

code = HttpStatus.MOVED_TEMPORARILY.value();

super.sendRedirect(location);

}


@Override

public void setStatus(int sc) {

this.code = sc;

}


@Override

public void setStatus(int sc, String message) {

this.code = sc;

}


@Override

public void sendError(int sc) throws IOException {

this.code = sc;

}


@Override

public void sendError(int sc, String msg) throws IOException {

this.code = sc;

}

}


static class ByteOutputStream extends ServletOutputStream {

private ByteArrayOutputStream bos = new ByteArrayOutputStream();


@Override

public void write(int b) throws IOException {

bos.write(b);

}


public byte[] getBytes() {

return bos.toByteArray();

}

}

}


本文出自 “重新來學JAVA” 部落格,請務必保留此出處http://3131854.blog.51cto.com/3121854/1637831

修改springmvc返回ajax方式的json資料

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.