4

I'm having a simple REST controller using spring. How could GZIP response of the returned application/xml stream be enabled?

@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 @GZIP annotation on the service class. How can I achieve the same for REST?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
membersound
  • 81,582
  • 193
  • 585
  • 1,120

1 Answers1

12

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

Here is a similar 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
Community
  • 1
  • 1
Paul John
  • 1,626
  • 1
  • 13
  • 15
  • 1
    It should be noted that this feature is NOT yet released (`1.2.2.RELEASE` will contain it). – membersound Feb 17 '15 at 08:30
  • 1
    Additionally, Spring MVC is [scheduled to add *automatic* gzip encoding handling to the `DispatcherServlet`](https://jira.spring.io/browse/SPR-11714) in 4.2. – chrylis -cautiouslyoptimistic- Feb 17 '15 at 08:50
  • @Membersound How to do it on having WildFly ? – Tomasz Waszczyk Jan 12 '16 at 08:19
  • @TomaszWaszczyk-PantaRhei you should ask a separate question on this. But I'm pretty sure the `server.tomcat.*` properties can't be used here. – membersound Jan 12 '16 at 11:23
  • My Spring Boot does not use the embedded Tomcat container and I have to deploy the WAR of my application to an existing Tomcat server instance. How do I enable gzip compression in this case? Do I need to implement a gzip filter specifically? – Web User Jan 16 '17 at 08:36