The issue you're encountering with Feign Client stems from its automatic encoding of path and query parameters. Feign uses Java's URLEncoder for encoding, which encodes the % character as %25. This can indeed cause issues when your API expects to receive the % character unencoded.
Approach to Handle Special Characters with Feign Client:
Here are a couple of solutions to address this problem:
1. Custom Request Interceptor
You can create a custom request interceptor to modify the outgoing request before it's sent. This way, you can manually handle specific parameters. However, keep in mind that this approach requires you to selectively encode parameters based on certain conditions.
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;
@Component
public class FeignCustomInterceptor implements RequestInterceptor {
@Override
public void apply(RequestTemplate template) {
// Example: modify the specific query parameter
// Look for specific query parameter
String param = template.queries().get("param").get(0);
if (param.contains("%")) {
// Don't encode it; you may need to handle the encoding manually as per your logic
template.query("param", param);
}
}
}
2. Use Raw URL Encoding
Feign supports the use of raw parameters by allowing you to define the parameters without the typical handling:
import feign.Param;
import feign.RequestLine;
import org.springframework.cloud.openfeign.FeignClient;
@FeignClient(name = "yourFeignClient")
public interface YourFeignClient {
@RequestLine("GET /your/api?param={param}")
String getByParam(@Param(value = "param") String param);
}
In this approach, you bypass some of Feign's automatic encoding mechanisms by specifying that the parameter should be used as-is.
3. Encode the Parameter Manually Before Sending
You can manually encode your parameter before passing it to the Feign Client to ensure it sends it in the expected format. However, if you do this, you need to be very careful about encoding when needed.
Example:
public String callFeignClient() {
String param = "abc%def"; // This is the actual value you want to send.
return yourFeignClient.getByParam(param);
}
4. Use a Feign Configuration
You can also try adding a specific configuration to your Feign client to customize the encoding behavior if the above methods don't lead to the desired result.
Conclusion
In general, Feign's behavior might be different from other clients (like Swagger UI, where special characters are treated differently). You may need to tailor the way parameters are sent specifically for your use case.
If none of these approaches resolve your issue, consider reaching out to the Spring Cloud or Feign community for more nuanced strategies or updates, as specific use cases sometimes require deeper alterations of library behaviors.