question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Enable compression support via configuration

See original GitHub issue

Hey,

While trying to implement the spring-cloud gateway as a proxy to a JSON API i have the following scenario for a route:

.route(r -> r.path("/foo")
                        .filters(f -> f.stripPrefix(1).prefixPath("/bar").modifyResponseBody(List.class, List.class, (serverWebExchange, s) -> {
                            // add or remove some stuff from s here
                            return s;
                        }))
                        .uri("http://foo.bar")
                )

However, with this default configuration i get the error:

org.springframework.core.codec.DecodingException: JSON decoding 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
 at [Source: UNKNOWN; line: 1, column: 2]
	at org.springframework.http.codec.json.Jackson2Tokenizer.tokenize(Jackson2Tokenizer.java:103) ~[spring-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]
	at reactor.core.publisher.FluxMapSignal$FluxMapSignalSubscriber.onNext(FluxMapSignal.java:137) ~[reactor-core-3.1.7.RELEASE.jar:3.1.7.RELEASE]
	at reactor.core.publisher.FluxMap$MapSubscriber.onNext(FluxMap.java:108) ~[reactor-core-3.1.7.RELEASE.jar:3.1.7.RELEASE]

I suspect that this is because the answer comes back with content-type:application/json but also with content-encoding:gzip, however the filter only uses the content-type in selecting a HTTP reader. This results in the reader trying to parse the gzipped content as plain JSON, and failing.

Please modify the modifyResponseBody filter to take content-encoding into account or add a way to give parsing hints to the http reader. Looking at the source code i saw that there already are some hints, but are not used for this purpose at this time and they cannot be initialized from the modifyResponseBody

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
barrenaeducommented, May 31, 2019

Hi, same problem here, what I have done (but does not work):

In build.gradle: implementation 'io.projectreactor.ipc:reactor-netty:0.7.15.RELEASE'

In java code: ` @Configuration public class NettyHttpClientConfiguration {

@Primary
@Bean
HttpClient httpClient(@Qualifier("nettyClientOptions") Consumer<? super HttpClientOptions.Builder> options) {
	return HttpClient.create(options.andThen(this::enableCompressionSupport));
}

private void enableCompressionSupport(Object builder) {
	((HttpClientOptions.Builder) builder).compression(true);
}

}`

When I try to run I get:


APPLICATION FAILED TO START


Description:

The bean ‘httpClient’, defined in class path resource [org/springframework/cloud/gateway/config/GatewayAutoConfiguration$NettyConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [com/app/gateway/config/NettyHttpClientConfiguration.class] and overriding is disabled.

Action:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

@EdgarsM do you know hoy to fix the httpClient bean duplicity? @primary is not working 😢

Any help would be appreciated.

0reactions
jacob2221commented, Nov 30, 2019

Any leads on this issue? Getting same issue with Greenwich.SR3. Tried below in @Configuration but not effect -

@Bean
    public Consumer<? super HttpClientOptions.Builder> nettyClientOptions() {
        return opts -> {
            opts.poolResources(PoolResources.elastic("proxy"));
        };
    }
    
    @Bean
    HttpClient httpClient(@Qualifier("nettyClientOptions") Consumer<? super HttpClientOptions.Builder> options) {
        return HttpClient.create(options.andThen(this::enableCompressionSupport));
    }

    private void enableCompressionSupport(Object builder) {
        ((HttpClientOptions.Builder) builder).compression(true);
    }
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Enable GZIP Compression to Speed Up WordPress Sites
Learn how to enable GZIP compression to speed up your WordPress site on various web servers like Apache, Nginx, and IIS.
Read more >
How to configure gzip compression with NGINX | TechRepublic
Here's an explanation for the configuration, line by line: gzip on; – enables gzip compression; gzip_vary on: – tells proxies to cache both...
Read more >
New – Gzip Compression Support for Amazon CloudFront
You can enable this feature in a minute! Simply open up the CloudFront Console, locate your distribution, and set Compress Objects Automatically ...
Read more >
How To Optimize Your Site With GZIP Compression
There are ways to pre-compress static content and send over the compressed versions. This requires more configuration; even if it's not possible, compressing...
Read more >
Compression and Decompression | NGINX Plus
To enable compression, include the gzip directive with the on parameter. gzip on;. By default, NGINX compresses responses only with MIME type text/html ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found