【问题标题】:Gzip specific controller in Spring MVCSpring MVC 中的 Gzip 特定控制器
【发布时间】:2017-09-13 07:48:43
【问题描述】:

有没有办法在 spring 中为一个方法或控制器添加 Gzip

@RequestMapping(value = "/system", method = {RequestMethod.GET})
@Gzip //<- Something similar this,
public ApiResponse status() throws Exception{

}

我不想使用 tomcat 配置为整个服务器启用它,因为我的客户端还没有准备好使用 gzip,

【问题讨论】:

标签: java spring rest spring-mvc gzip


【解决方案1】:

在将内容写入客户端之前,您可以将 java.io.OutputStreamjavax.servlet.http.HttpServletResponse(用于特定 gzip HTTP 标头)作为参数用 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
   }
}

【讨论】:

    猜你喜欢
    • 2011-11-16
    • 2023-03-22
    • 2018-11-11
    • 2017-09-21
    • 1970-01-01
    • 1970-01-01
    • 2018-04-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多