using spring boot open feign, i want to use a % character as a request param without it…
- User
using spring boot open feign, i want to use a % character as a request param without it been encoded by feign. I use a RequestInterceptor where i specifically replace every "%25" in the uri as "%" only but the call is still showing an encoded % character in the url and therefore not working. What can i do?
- Assistant
To correctly send a
%character in a URL using Spring Boot's OpenFeign client without it being encoded as%25, you need to manage how your parameters are handled before they are encoded and sent over the wire. Here are a few strategies you can consider:Option 1: Custom Encoder
The best way to manage raw parameters with OpenFeign is to implement a custom encoder. OpenFeign uses the default
Feignencoder which encodes parameters according to standard URL encoding rules. To avoid this, you need a custom encoder that skips encoding for specific characters.- Create a Custom Encoder:
import feign.codec.Encoder; import feign.Util; import java.io.IOException; import java.lang.reflect.Type; import java.nio.charset.Charset; import java.util.Map; public class CustomEncoder implements Encoder { private final Encoder defaultEncoder; public CustomEncoder(Encoder defaultEncoder) { this.defaultEncoder = defaultEncoder; } @Override public void encode(Object object, Type bodyType, Map<String, Object> params) throws IOException { // If object is a string, replace %25 with % if (object instanceof String) { String value = (String) object; value = value.replace("%25", "%"); // Pass the modified value to the default encoder defaultEncoder.encode(value, bodyType, params); } else { defaultEncoder.encode(object, bodyType, params); } } }- Use the Custom Encoder in your Feign Configuration:
In your Feign client configuration, you can specify the custom encoder:
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import feign.codec.Decoder; import feign.codec.Encoder; @Configuration public class FeignConfig { @Bean public Encoder feignEncoder() { return new CustomEncoder(new Encoder.Default()); } // You can add other Feign beans like Decoder, Logger etc. if needed. }- Apply the Configuration to Your Feign Client:
When defining your Feign client, make sure to reference this configuration class:
import org.springframework.cloud.openfeign.FeignClient; @FeignClient(name = "yourClientName", url = "http://your.api/url", configuration = FeignConfig.class) public interface YourFeignClient { // Define your method with request params that may contain '%' @RequestLine("GET /your-endpoint?param={param}") ResponseType yourMethod(@Param("param") String param); }Option 2: Encode Manually
If the custom encoder is too complex for your case, you can try manually encoding the parameters in a way that Feign uses when sending the request.
public String encodeWithCustomEncoding(String param) { // Replace % with its desired form before it gets encoded by Feign. return param.replace("%", "<YOUR_CUSTOM_REPLACEMENT>"); }Then, when calling your Feign client, you need to call
encodeWithCustomEncoding:String encodedParam = encodeWithCustomEncoding(yourParam); yourFeignClient.yourMethod(encodedParam);Conclusion
Using a custom encoder is the most elegant solution if you want to retain full control over how parameters are encoded. By modifying the encoder, you can define exactly what gets sent in the request while leveraging Feign's capabilities.
Make sure to test each approach extensively to confirm that parameters are formatted correctly as expected by the API you’re calling.
- User
i have several feignclient but they all use this new configuration even if i don't specify it in the @FeignClient annotation ! What is the name of the default configuration for springboot ?
- Assistant
In Spring Boot, when you're using OpenFeign, the default configuration for all Feign clients is managed by the
FeignClientsConfigurationclass. This configuration automatically applies settings such as encoders, decoders, and error handlers to all Feign clients unless you override them with a specific configuration.The
FeignClientsConfigurationclass is part of Spring Cloud OpenFeign, and it's where various default settings are established for how Feign clients behave.Default Configuration
To clarify, here’s how the defaults are set up:
-
Global Configuration: If you have a
@EnableFeignClientsannotation in your Spring Boot application, the default settings fromFeignClientsConfigurationapply to all Feign clients in the application context. -
Example of Using Default Configuration:
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.context.annotation.Configuration; @Configuration @EnableFeignClients public class ApplicationConfig { // Other configurations, if needed } @FeignClient(name = "exampleClient", url = "http://example.com") public interface ExampleFeignClient { // Define methods for the Feign client } -
Overriding Default Configuration: If you want specific Feign clients to use custom configurations, you can create a configuration class and specify it in your
@FeignClientannotation:@FeignClient(name = "exampleClient", url = "http://example.com", configuration = CustomFeignConfig.class) public interface ExampleFeignClient { // Define methods for the Feign client } -
Preventing Global Configuration: If you want specific Feign clients to avoid the globally set configurations, you must define a configuration class for those clients. Simply not specifying a configuration in the
@FeignClientannotation will result in pulling defaults fromFeignClientsConfiguration.
Summary
- The default configuration for Spring Cloud OpenFeign is handled by
FeignClientsConfiguration. - If you use a custom configuration class, it should be explicitly specified in the
@FeignClientannotation to override defaults for a specific Feign client. - If you want to control the configuration on a per-client basis, you should create custom configuration classes and reference them explicitly in your Feign client declarations.
If you want to apply custom configurations only to specific Feign clients, ensure you specify those configurations in each
@FeignClientas needed. All Feign clients will use the defaults unless you explicitly define otherwise. -