Querying a web service can result to huge responses, e.g. the response of a SOS GetObservation request. To reduce the response size HTTP offers the possibility to compress the response content.

Clients that support compressed responses can indicate this by adding the Accept-Encoding parameter with value gzip to the HTTP header of the request. If the response is compressed, the compression is indicated by the parameter Content-Encoding with value gzip in the HTTP header of the response.

Server

Using servlet container

Servlet containers often have out of the box compression support, please check the documentation.

Leveraging HTTP server

If your web service is behind an HTTP server such as nginx or Apache, it might be a good solution to leave the compression to the webserver instead of putting the load on the application or Servlet container.

Standalone implementation in a Java web application

Here is an example for the response content compression support. It checks if the indicator for response compression is set in the HTTP header. If the client supports compression and the response size is greater than 1000000 the service compresses the response and adds the compression indicator to the HTTP header of the response.

Example of a HttpServlet class with doPost method that contains the response compression funtionality