Code Monkey home page Code Monkey logo

spring-boot-gzip-compression-example's Introduction

Spring Boot Gzip Compression Example

Simple example that demonstrates how to configure Gzip compression for HTTP responses in a Spring Boot 2.x microservice.

Requirements

  • java 8u181 or better
  • cf CLI 6.38.0 or better

To get the code

$ git clone https://github.com/pacphi/spring-boot-gzip-compression-example.git

If this is your first time using Github, review http://help.github.com to learn the basics.

To enable the /citylots.json endpoint

From a shell

$ cd spring-boot-gzip-compression-example
$ cd src/main/resources
$ mkdir static
$ cd static
$ git clone https://github.com/zemirco/sf-city-lots-json
$ mv sf-city-lots-json/citylots.json .
$ rm -Rf sf-city-lots-json
$ cd ../../../../

To run the application

From a shell

$ cd spring-boot-gzip-compression-example
$ ./gradlew clean bootRun 

To deploy to Cloud Foundry

e.g. on Pivotal Web Services

$ cf login -a api.run.pivotal.io
$ cf push 

To verify response is compressed

Locally

$ curl -H "Accept-Encoding: gzip" http://localhost:8080/citylots.json > citylots.json.gz
$ curl -H "Content-Type: application/json" http://localhost:8080/actuator/httptrace
{
  "traces": [
    {
      "timestamp": "2018-08-29T00:27:30.284200Z",
      "principal": null,
      "session": null,
      "request": {
        "method": "GET",
        "uri": "http://localhost:8080/citylots.json",
        "headers": {
          "host": [
            "localhost:8080"
          ],
          "accept-encoding": [
            "gzip"
          ],
          "user-agent": [
            "curl/7.54.0"
          ],
          "accept": [
            "*/*"
          ]
        },
        "remoteAddress": null
      },
      "response": {
        "status": 200,
        "headers": {
          "Accept-Ranges": [
            "bytes"
          ],
          "Transfer-Encoding": [
            "chunked"
          ],
          "Content-Encoding": [
            "gzip"
          ],
          "Vary": [
            "Accept-Encoding"
          ],
          "Last-Modified": [
            "Wed, 29 Aug 2018 00:26:23 GMT"
          ],
          "Date": [
            "Wed, 29 Aug 2018 00:27:30 GMT"
          ],
          "Content-Type": [
            "application/json"
          ]
        }
      },
      "timeTaken": 3307
    }
  ]
}

on PWS

$ curl -H "Accept-Encoding: gzip" http://spring-boot-gzip-compression-example-active-meerkat.cfapps.io/citylots.json > citylots.json.gz 
$ curl -H "Content-Type: application/json" http://spring-boot-gzip-compression-example-active-meerkat.cfapps.io/actuator/httptrace
{
  "traces": [
    {
      "timestamp": "2018-08-29T00:30:28.641Z",
      "principal": null,
      "session": null,
      "request": {
        "method": "GET",
        "uri": "http://spring-boot-gzip-compression-example-active-meerkat.cfapps.io/citylots.json",
        "headers": {
          "x-cf-applicationid": [
            "54cd4b9f-71a8-4ce2-b457-79cc32e5f368"
          ],
          "x-forwarded-proto": [
            "http"
          ],
          "x-b3-traceid": [
            "214b30441344844d"
          ],
          "x-b3-spanid": [
            "214b30441344844d"
          ],
          "x-cf-instanceid": [
            "c6fe8b5a-f417-415d-6c8c-314a"
          ],
          "x-vcap-request-id": [
            "78c2ebe5-b5c0-4f76-66a5-0e8cee2f8563"
          ],
          "x-request-start": [
            "1535502627509"
          ],
          "host": [
            "spring-echo-example-terrific-badger.cfapps.io"
          ],
          "x-forwarded-port": [
            "80"
          ],
          "x-cf-instanceindex": [
            "0"
          ],
          "accept-encoding": [
            "gzip"
          ],
          "accept": [
            "*/*"
          ],
          "user-agent": [
            "curl/7.54.0"
          ]
        },
        "remoteAddress": null
      },
      "response": {
        "status": 200,
        "headers": {
          "Transfer-Encoding": [
            "chunked"
          ],
          "Accept-Ranges": [
            "bytes"
          ],
          "Content-Encoding": [
            "gzip"
          ],
          "Vary": [
            "Accept-Encoding"
          ],
          "Last-Modified": [
            "Wed, 29 Aug 2018 00:26:52 GMT"
          ],
          "Date": [
            "Wed, 29 Aug 2018 00:30:28 GMT"
          ],
          "Content-Type": [
            "application/json"
          ]
        }
      },
      "timeTaken": 9796
    }
  ]
}

License

Released under the Apache 2.0 license

Credits

  • Thanks to Ben Hale for the /actuator/httptrace tip (I used for verification purposes).

spring-boot-gzip-compression-example's People

Contributors

dependabot-preview[bot] avatar dependabot-support avatar pacphi avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.