Java 如何为 RestController 启用 GZIP?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28557165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 06:25:21  来源:igfitidea点击:

How to enable GZIP for RestController?

javaspringrestspring-bootgzip

提问by membersound

I'm having a simple RESTcontroller using spring. How could GZIP response of the returned application/xmlstream be enabled?

我有一个简单的REST控制器,使用spring. 如何application/xml启用返回流的GZIP 响应?

@RestController
public class MyRest {
    @RequestMapping(method = RequestMethod.GET,
            produces = MediaType.APPLICATION_XML_VALUE)
    @ResponseBody
    public ComplexRsp test() {
        //...
    }
}

When using soap+wsdl approach, there would be the simple @GZIPannotation on the service class. How can I achieve the same for REST?

使用soap+wsdl方式时,@GZIP服务类上会有简单的注解。如何为 REST 实现相同的目标?

采纳答案by Paul John

If you are using Spring boot and Tomcat You should be able to accomplish this via Tomcat Configuration: http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#how-to-enable-http-response-compression

如果您使用的是 Spring Boot 和 Tomcat 您应该能够通过 Tomcat 配置来完成此操作:http: //docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#how-to-enable- http-响应-压缩

Here is a similar POST Using GZIP compression with Spring Boot/MVC/JavaConfig with RESTful

这是一个类似的 POST Using GZIP compression with Spring Boot/MVC/JavaConfig with RESTful

It's as simple as follows:

它很简单,如下所示:

server.compression.enabled=true
server.compression.mime-types=application/xml