In Spring Boot, when using Spring Cloud OpenFeign, you may encounter issues with URL encoding, particularly if you’re trying to send special characters in request parameters without them being escaped.
By default, Spring's RestTemplate and OpenFeign escape certain characters in the URL, which can lead to issues like the one you described, where % becomes %25. To handle this, you can customize the way that URL encoding is done in your Feign client.
You can do this by defining a custom Contract and overriding the way it encodes the parameters. Here is a step-by-step guide on how to achieve this:
Step 1: Create a Custom Contract
Create a custom contract class that extends FeignContract and overrides the encoding behavior. This way, you can define how parameters are encoded before sending them.
import feign.Contract;
import feign.RequestTemplate;
public class CustomFeignContract extends Contract {
@Override
public void apply(RequestTemplate template) {
// Custom logic to handle URL encoding here
// This example disables the encoding for specific characters
String originalUrl = template.url();
// Replace % with a placeholder or remove character escaping logic
String modifiedUrl = originalUrl.replace("%25", "%"); // or handle as required
template.uri(modifiedUrl);
// Now call the super to allow the contract to function as normal
super.apply(template);
}
}
Step 2: Configure OpenFeign to Use Your Custom Contract
You will need to configure your Feign clients to use your custom contract. You can do this in your Spring Boot application configuration class.
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.cloud.openfeign.FeignClientsConfiguration;
@Configuration
public class FeignConfig extends FeignClientsConfiguration {
@Bean
public Contract feignContract() {
return new CustomFeignContract();
}
}
Step 3: Apply Configuration to Your Feign Client
Make sure your Feign client is using this configuration:
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient(name = "myFeignClient", url = "http://example.com", configuration = FeignConfig.class)
public interface MyFeignClient {
@GetMapping("/endpoint")
String callEndpoint(@RequestParam("param") String param);
}
Step 4: Use Your Feign Client as Needed
Now you should be able to call your Feign client and pass in parameters like abc%def without them being escaped.
Important Note
While the above solution works, you should carefully consider the implications of disabling encoding for special characters. Correctly encoding URLs is important for security and behavior on HTTP clients. Evaluate whether you can achieve your goal without bypassing URL encoding or if there's a better approach to structuring your request.
Remember that using special characters in URLs can lead to multiple issues, including unexpected behavior in the receiving service or client. Always test extensively when modifying defaults related to parameter encoding.