Browse Source

Overridable QueryMapEncoder (#183)

Added the possibility to override the default QueryMapEncoder by defining a custom one in the ApplicationContext.
pull/140/head
Jánoky László Viktor 6 years ago committed by Spencer Gibb
parent
commit
3a60921794
  1. 3
      docs/src/main/asciidoc/spring-cloud-openfeign.adoc
  2. 6
      spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/FeignClientFactoryBean.java
  3. 20
      spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/FeignClientOverrideDefaultsTests.java

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

@ -128,6 +128,7 @@ Spring Cloud Netflix _does not_ provide the following beans by default for feign @@ -128,6 +128,7 @@ Spring Cloud Netflix _does not_ provide the following beans by default for feign
* `Request.Options`
* `Collection<RequestInterceptor>`
* `SetterFactory`
* `QueryMapEncoder`
Creating a bean of one of those type and placing it in a `@FeignClient` configuration (such as `FooConfiguration` above) allows you to override each one of the beans described. Example:
@ -487,3 +488,5 @@ public class DemoTemplate { @@ -487,3 +488,5 @@ public class DemoTemplate {
String demoEndpoint(@SpringQueryMap Params params);
}
----
If you need more control over the generated query parameter map, you can implement a custom `QueryMapEncoder` bean.

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

@ -23,6 +23,7 @@ import feign.Client; @@ -23,6 +23,7 @@ import feign.Client;
import feign.Contract;
import feign.Feign;
import feign.Logger;
import feign.QueryMapEncoder;
import feign.Request;
import feign.RequestInterceptor;
import feign.Retryer;
@ -147,7 +148,10 @@ class FeignClientFactoryBean @@ -147,7 +148,10 @@ class FeignClientFactoryBean
if (requestInterceptors != null) {
builder.requestInterceptors(requestInterceptors.values());
}
QueryMapEncoder queryMapEncoder = getOptional(context, QueryMapEncoder.class);
if (queryMapEncoder != null) {
builder.queryMapEncoder(queryMapEncoder);
}
if (this.decode404) {
builder.decode404();
}

20
spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/FeignClientOverrideDefaultsTests.java

@ -19,6 +19,7 @@ package org.springframework.cloud.openfeign; @@ -19,6 +19,7 @@ package org.springframework.cloud.openfeign;
import feign.Contract;
import feign.Feign;
import feign.Logger;
import feign.QueryMapEncoder;
import feign.Request;
import feign.RequestInterceptor;
import feign.RequestLine;
@ -30,6 +31,7 @@ import feign.codec.Encoder; @@ -30,6 +31,7 @@ import feign.codec.Encoder;
import feign.codec.ErrorDecoder;
import feign.hystrix.HystrixFeign;
import feign.optionals.OptionalDecoder;
import feign.querymap.BeanQueryMapEncoder;
import feign.slf4j.Slf4jLogger;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -133,6 +135,14 @@ public class FeignClientOverrideDefaultsTests { @@ -133,6 +135,14 @@ public class FeignClientOverrideDefaultsTests {
assertThat(options.readTimeoutMillis()).isEqualTo(1);
}
@Test
public void overrideQueryMapEncoder() {
QueryMapEncoder.Default.class
.cast(this.context.getInstance("foo", QueryMapEncoder.class));
BeanQueryMapEncoder.class
.cast(this.context.getInstance("bar", QueryMapEncoder.class));
}
@Test
public void addRequestInterceptor() {
assertThat(this.context.getInstances("foo", RequestInterceptor.class).size())
@ -203,6 +213,11 @@ public class FeignClientOverrideDefaultsTests { @@ -203,6 +213,11 @@ public class FeignClientOverrideDefaultsTests {
return HystrixFeign.builder();
}
@Bean
public QueryMapEncoder queryMapEncoder() {
return new feign.QueryMapEncoder.Default();
}
}
public static class BarConfiguration {
@ -232,6 +247,11 @@ public class FeignClientOverrideDefaultsTests { @@ -232,6 +247,11 @@ public class FeignClientOverrideDefaultsTests {
return new BasicAuthRequestInterceptor("user", "pass");
}
@Bean
public QueryMapEncoder queryMapEncoder() {
return new BeanQueryMapEncoder();
}
}
}

Loading…
Cancel
Save