有没有办法在spring中为一个方法或控制器添加Gzip
@RequestMapping(value = "/system", method = {RequestMethod.GET})
@Gzip //<- Something similar this,
public ApiResponse status() throws Exception{
}
我不想使用tomcat配置为整个服务器启用它,因为我的客户尚未准备好使用gzip,
答案 0 :(得分:1)
在将内容写入客户端之前,您可以将java.io.OutputStream
或javax.servlet.http.HttpServletResponse
(对于特定的gzip HTTP标头)放入Controller方法作为参数包装java.util.zip.GZIPOutputStream
@RequestMapping(value = "/system", method = {RequestMethod.GET})
public void status(HttpServletResponse response) throws Exception {
try(GZIPOutputStream out = new GZIPOutputStream(response.getOutputStream())) {
// write to out the content
}
}