Skip to content

Commit cf20710

Browse files
Improve charset determination (#491)
1 parent c92849e commit cf20710

File tree

11 files changed

+229
-107
lines changed

11 files changed

+229
-107
lines changed
Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
11
|===
22
|Name | Default | Description
33

4-
|feign.circuitbreaker.enabled | false | If true, an OpenFeign client will be wrapped with a Spring Cloud CircuitBreaker circuit breaker.
4+
|feign.autoconfiguration.jackson.enabled | `false` | If true, PageJacksonModule and SortJacksonModule bean will be provided for Jackson page decoding.
5+
|feign.circuitbreaker.enabled | `false` | If true, an OpenFeign client will be wrapped with a Spring Cloud CircuitBreaker circuit breaker.
56
|feign.client.config | |
6-
|feign.client.decode-slash | true | Feign clients do not encode slash `/` characters by default. To change this behavior, set the `decodeSlash` to `false`.
7-
|feign.client.default-config | default |
8-
|feign.client.default-to-properties | true |
9-
|feign.compression.request.enabled | false | Enables the request sent by Feign to be compressed.
10-
|feign.compression.request.mime-types | [text/xml, application/xml, application/json] | The list of supported mime types.
11-
|feign.compression.request.min-request-size | 2048 | The minimum threshold content size.
12-
|feign.compression.response.enabled | false | Enables the response from Feign to be compressed.
13-
|feign.compression.response.useGzipDecoder | false | Enables the default gzip decoder to be used.
14-
|feign.httpclient.connection-timeout | 2000 |
15-
|feign.httpclient.connection-timer-repeat | 3000 |
16-
|feign.httpclient.disable-ssl-validation | false |
17-
|feign.httpclient.enabled | true | Enables the use of the Apache HTTP Client by Feign.
18-
|feign.httpclient.follow-redirects | true |
19-
|feign.httpclient.max-connections | 200 |
20-
|feign.httpclient.max-connections-per-route | 50 |
21-
|feign.httpclient.time-to-live | 900 |
7+
|feign.client.decode-slash | `true` | Feign clients do not encode slash `/` characters by default. To change this behavior, set the `decodeSlash` to `false`.
8+
|feign.client.default-config | `default` |
9+
|feign.client.default-to-properties | `true` |
10+
|feign.compression.request.enabled | `false` | Enables the request sent by Feign to be compressed.
11+
|feign.compression.request.mime-types | `[text/xml, application/xml, application/json]` | The list of supported mime types.
12+
|feign.compression.request.min-request-size | `2048` | The minimum threshold content size.
13+
|feign.compression.response.enabled | `false` | Enables the response from Feign to be compressed.
14+
|feign.compression.response.useGzipDecoder | `false` | Enables the default gzip decoder to be used.
15+
|feign.encoder.charset-from-content-type | `false` |
16+
|feign.httpclient.connection-timeout | `2000` |
17+
|feign.httpclient.connection-timer-repeat | `3000` |
18+
|feign.httpclient.disable-ssl-validation | `false` |
19+
|feign.httpclient.enabled | `true` | Enables the use of the Apache HTTP Client by Feign.
20+
|feign.httpclient.follow-redirects | `true` |
21+
|feign.httpclient.max-connections | `200` |
22+
|feign.httpclient.max-connections-per-route | `50` |
23+
|feign.httpclient.time-to-live | `900` |
2224
|feign.httpclient.time-to-live-unit | |
23-
|feign.hystrix.enabled | false | If true, an OpenFeign client will be wrapped with a Hystrix circuit breaker.
24-
|feign.okhttp.enabled | false | Enables the use of the OK HTTP Client by Feign.
25+
|feign.hystrix.enabled | `false` | If true, an OpenFeign client will be wrapped with a Hystrix circuit breaker.
26+
|feign.okhttp.enabled | `false` | Enables the use of the OK HTTP Client by Feign.
2527

2628
|===

docs/src/main/asciidoc/spring-cloud-openfeign.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,12 @@ public FeignClientConfigurer feignClientConfigurer() {
277277

278278
TIP: By default, Feign clients do not encode slash `/` characters. You can change this behaviour, by setting the value of `feign.client.decodeSlash` to `false`.
279279

280+
==== `SpringEncoder` configuration
281+
282+
In the `SpringEncoder` that we provide, we set `null` charset for binary content types and `UTF-8` for all the other ones.
283+
284+
You can modify this behaviour to derive the charset from the `Content-Type` header charset instead by setting the value of `feign.encoder.charset-from-content-type` to `true`.
285+
280286
[[timeout-handling]]
281287
=== Timeout Handling
282288

spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/FeignAutoConfiguration.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2020 the original author or authors.
2+
* Copyright 2013-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -55,6 +55,7 @@
5555
import org.springframework.cloud.commons.httpclient.OkHttpClientConnectionPoolFactory;
5656
import org.springframework.cloud.commons.httpclient.OkHttpClientFactory;
5757
import org.springframework.cloud.openfeign.support.DefaultGzipDecoderConfiguration;
58+
import org.springframework.cloud.openfeign.support.FeignEncoderProperties;
5859
import org.springframework.cloud.openfeign.support.FeignHttpClientProperties;
5960
import org.springframework.cloud.openfeign.support.PageJacksonModule;
6061
import org.springframework.cloud.openfeign.support.SortJacksonModule;
@@ -71,11 +72,12 @@
7172
* @author Grzegorz Poznachowski
7273
* @author Nikita Konev
7374
* @author Tim Peeters
75+
* @author Olga Maciaszek-Sharma
7476
*/
7577
@Configuration(proxyBeanMethods = false)
7678
@ConditionalOnClass(Feign.class)
7779
@EnableConfigurationProperties({ FeignClientProperties.class,
78-
FeignHttpClientProperties.class })
80+
FeignHttpClientProperties.class, FeignEncoderProperties.class })
7981
@Import(DefaultGzipDecoderConfiguration.class)
8082
public class FeignAutoConfiguration {
8183

spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/FeignClientsConfiguration.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2020 the original author or authors.
2+
* Copyright 2013-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -45,6 +45,7 @@
4545
import org.springframework.cloud.client.circuitbreaker.CircuitBreakerFactory;
4646
import org.springframework.cloud.openfeign.clientconfig.FeignClientConfigurer;
4747
import org.springframework.cloud.openfeign.support.AbstractFormWriter;
48+
import org.springframework.cloud.openfeign.support.FeignEncoderProperties;
4849
import org.springframework.cloud.openfeign.support.PageableSpringEncoder;
4950
import org.springframework.cloud.openfeign.support.ResponseEntityDecoder;
5051
import org.springframework.cloud.openfeign.support.SpringDecoder;
@@ -63,6 +64,7 @@
6364
* @author Dave Syer
6465
* @author Venil Noronha
6566
* @author Darren Foong
67+
* @author Olga Maciaszek-Sharma
6668
*/
6769
@Configuration(proxyBeanMethods = false)
6870
public class FeignClientsConfiguration {
@@ -85,6 +87,9 @@ public class FeignClientsConfiguration {
8587
@Autowired(required = false)
8688
private FeignClientProperties feignClientProperties;
8789

90+
@Autowired(required = false)
91+
private FeignEncoderProperties encoderProperties;
92+
8893
@Bean
8994
@ConditionalOnMissingBean
9095
public Decoder feignDecoder() {
@@ -96,7 +101,7 @@ public Decoder feignDecoder() {
96101
@ConditionalOnMissingBean
97102
@ConditionalOnMissingClass("org.springframework.data.domain.Pageable")
98103
public Encoder feignEncoder(ObjectProvider<AbstractFormWriter> formWriterProvider) {
99-
return springEncoder(formWriterProvider);
104+
return springEncoder(formWriterProvider, encoderProperties);
100105
}
101106

102107
@Bean
@@ -105,7 +110,7 @@ public Encoder feignEncoder(ObjectProvider<AbstractFormWriter> formWriterProvide
105110
public Encoder feignEncoderPageable(
106111
ObjectProvider<AbstractFormWriter> formWriterProvider) {
107112
PageableSpringEncoder encoder = new PageableSpringEncoder(
108-
springEncoder(formWriterProvider));
113+
springEncoder(formWriterProvider, encoderProperties));
109114

110115
if (springDataWebProperties != null) {
111116
encoder.setPageParameter(
@@ -162,15 +167,17 @@ public FeignClientConfigurer feignClientConfigurer() {
162167
};
163168
}
164169

165-
private Encoder springEncoder(ObjectProvider<AbstractFormWriter> formWriterProvider) {
170+
private Encoder springEncoder(ObjectProvider<AbstractFormWriter> formWriterProvider,
171+
FeignEncoderProperties encoderProperties) {
166172
AbstractFormWriter formWriter = formWriterProvider.getIfAvailable();
167173

168174
if (formWriter != null) {
169175
return new SpringEncoder(new SpringPojoFormEncoder(formWriter),
170-
this.messageConverters);
176+
this.messageConverters, encoderProperties);
171177
}
172178
else {
173-
return new SpringEncoder(new SpringFormEncoder(), this.messageConverters);
179+
return new SpringEncoder(new SpringFormEncoder(), this.messageConverters,
180+
encoderProperties);
174181
}
175182
}
176183

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2013-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.openfeign.support;
18+
19+
import org.springframework.boot.context.properties.ConfigurationProperties;
20+
21+
/**
22+
* Properties for {@link SpringEncoder}.
23+
*
24+
* @author Olga Maciaszek-Sharma
25+
*
26+
* @since 2.2.8
27+
*/
28+
@ConfigurationProperties("feign.encoder")
29+
public class FeignEncoderProperties {
30+
31+
/**
32+
* Indicates whether the charset should be derived from the {@code Content-Type} header.
33+
*/
34+
private boolean charsetFromContentType = false;
35+
36+
public boolean isCharsetFromContentType() {
37+
return charsetFromContentType;
38+
}
39+
40+
public void setCharsetFromContentType(boolean charsetFromContentType) {
41+
this.charsetFromContentType = charsetFromContentType;
42+
}
43+
44+
}

0 commit comments

Comments
 (0)