2017-09-19 3 views
5

Ich habe eine Spring Boot REST App (1.5.6.RELEASE). Ich möchte Gzip-Komprimierung ein- und ausgehend. Gemäß dieser Dokumentation https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html Ich habeBehandlung von gezippten Anfragen in einer Spring Boot REST Applikation

gesetzt
server.compression.enabled=true 
server.compression.mime-types=... 

Aber das scheint nur gelten Antworten von meinem Dienst Gzipping (und das ist, was die Doc sagt tatsächlich „# Wenn Antwort Komprimierung aktiviert ist.“).

Mein Problem ist, dass eingehenden gziped Anforderungen nicht dekomprimiert werden, was zu JSON-Parsing-Fehler führt.

Weiß jemand wie ich Dekomprimierung in meiner Spring Boot App einschalten kann?

EDIT Ein Beispiel:

POM-Schnipsel:

<parent> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-parent</artifactId> 
    <version>1.5.6.RELEASE</version> 
</parent> 
<dependencies> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-web</artifactId> 
    </dependency> 
</dependencies> 

Controller-Code:

@RestController 
public class Controller { 
    @RequestMapping(value = "/", method = RequestMethod.POST, consumes = "application/json") 
    public String post(@RequestBody Map<String, String> request) { 
     return request.get("key"); 
    } 
} 

Test mit curl:

$ echo '{ "key":"hello" }' > body 
$ curl -X POST -H "Content-Type: application/json" --data-binary @body http://localhost:8080 # prints 'hello' 
$ echo '{ "key":"hello" }' | gzip > body.gz 
$ curl -X POST -H "Content-Type: application/json" -H "Content-Encoding: gzip" --data-binary @body.gz http://localhost:8080 # fails 

Der gzip-Aufruf schlägt fehl mit der Meldung:

{"timestamp":1505843443456,"status":400,"error":"Bad Request","exception":"org.springframework.http.converter.HttpMessageNotReadableException","message":"JSON parse error: Illegal character ((CTRL-CHAR, code 31)): only regular white space (\\r, \\n, \\t) is allowed between tokens; nested exception is com.fasterxml.jackson.core.JsonParseException: Illegal character ((CTRL-CHAR, code 31)): only regular white space (\\r, \\n, \\t) is allowed between tokens\n at [Source: [email protected]; line: 1, column: 2]","path":"/"} 
+0

Wie stellen Sie komprimierte Anfragen aus? – diginoise

+0

Siehe https://stackoverflow.com/q/20507007/46673, https://stackoverflow.com/q/16638345/466738, https://serverfault.com/questions/56700/is-it-possible-to- enable-http-compression-for-requests –

+0

@diginoise Ich kontrolliere den Client nicht – B255

Antwort

0

Die server.compression.* Konfigurationsschlüssel über HTTP-Response-Komprimierung sind nur. Mir ist keine allgemeine Lösung bekannt und auch nicht, wenn Server dies nativ unterstützen.

Sie können dies unterstützen, indem Sie einen Servlet-Filter verwenden, der genau das tut, aber Spring Boot bietet diese Funktion nicht an.

+0

oder du stellst deinen Servlet Container hinter Apache und lass Apache damit umgehen: https://serverfault.com/questions/ 56700/is-it-possible-enable-http-compression-for-requests # antwort-56707 – Adam

+0

Oder benutze nginx oder benutze eine cdn oder ... –

+0

naja, ich habe ein paar Fragen dazu bekommen ob nginx das macht oder nicht - https://serverfault.com/questions/334215/how-do-i-configure-nginx-to-accept-gzip-requests - von 2011. Aber ich konnte nichts neueres finden, das besagt, dass nginx jetzt Anfrage erlaubt Komprimierung (Suche über 'content-encoding' über die nginx-Dokumentation) – Adam

Verwandte Themen