如何为RestController启用GZIP?

时间:2015-02-17 07:56:39

标签: java spring rest spring-boot gzip

我使用REST设置了一个简单的spring控制器。 如何启用返回的application/xml流的GZIP响应?

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

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

1 个答案:

答案 0 :(得分:11)

如果您正在使用Spring启动和Tomcat您应该能够通过Tomcat配置完成此任务: http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#how-to-enable-http-response-compression

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

这很简单如下:

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