thymeleaf 压缩; springboot 响应压缩; java response修改内容; 修改 response 内容

下面三处,复制到自己的代码里就可以使用了!!!

pom.xml


	com.googlecode.htmlcompressor
	htmlcompressor
	1.5.2



	com.yahoo.platform.yui
	yuicompressor
	2.4.6

ResponseWrapper.class 

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.io.CharArrayWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;

public class ResponseWrapper extends HttpServletResponseWrapper {
    private PrintWriter cachedWriter;
    private CharArrayWriter bufferedWriter;


    public ResponseWrapper(HttpServletResponse response) {
        super(response);
        // 这个是我们保存返回结果的地方
        bufferedWriter = new CharArrayWriter();
        // 这个是包装PrintWriter的,让所有结果通过这个PrintWriter写入到bufferedWriter中
        cachedWriter = new PrintWriter(bufferedWriter);
    }


    @Override
    public PrintWriter getWriter() throws IOException {
        return cachedWriter;
    }


    /**
     * 获取原始的HTML页面内容。
     */
    public String getResult() {
        byte[] bytes = bufferedWriter.toString().getBytes();
        cachedWriter.close();
        bufferedWriter.close();
        return new String(bytes, StandardCharsets.UTF_8);
    }
}

 HtmlCompressorFilter.class

import com.googlecode.htmlcompressor.compressor.HtmlCompressor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.ServletComponentScan;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Slf4j
@ServletComponentScan
@WebFilter(filterName = "HtmlCompressorInterceptor", urlPatterns = "/")
public class HtmlCompressorFilter implements Filter {
    private HtmlCompressor htmlCompressor;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        htmlCompressor = new HtmlCompressor();
        htmlCompressor.setCompressCss(true);
        htmlCompressor.setCompressJavaScript(true);
        htmlCompressor.setRemoveComments(true);
        htmlCompressor.setRemoveIntertagSpaces(true);
        htmlCompressor.setRemoveMultiSpaces(true);
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        // 使用我们自定义的响应包装器来包装原始的ServletResponse
        ResponseWrapper wrapper = new ResponseWrapper((HttpServletResponse) response);
        // 这里一定要写成 wrapper
        chain.doFilter(request, wrapper);
        // 重置响应输出的内容长度
        response.setContentLength(-1);
        PrintWriter out = response.getWriter();
        // 压缩
        out.write(htmlCompressor.compress(wrapper.getResult()));
        // 刷新
        out.flush();
    }

    @Override
    public void destroy() {

    }

}

参考:https://www.nosuchfield.com/2016/03/29/gets-the-servlet%27s-response-and-modifies-its-contents/

你可能感兴趣的:(Java,java,spring,spring,boot,servlet)