I am trying to use Spring boot to communicate with a backend server which does not support encoded URL. I tried intercepting the RestTemplate and modifying the query parameter but it does not seems to work. What should be the proper way to doing it?
The code for feign client is
@FeignClient(url = "${gateway.api}",
configuration = BackendConfig.class)
@RequestMapping("/v1/")
public interface GatewayClient {
@GetMapping(path = "/authorize")
String getAuthorization(@RequestParam(name = "cburl") String url);
}
Now if I invoke GatewayClient.authorize("http://example.com") I can see that it gets called ${gateway.api}/v1/authorize?cburl=http:%2F%2Fexample.com which is not recognized by the backend service. However, $(gateway.api}/v1/authorize?cburl=http://example.com works.
The BackendConfig class is given below for reference
class BackendConfig {
@Autowired
ObjectFactory<HttpMessageConverters> messageConverters;
@Bean
public Decoder springDecoder() { return new ResponseEntityDecover(new SpringDecoder(messageConverters); }
@Bean
public MyInterceptor requestInterceptor() {
return new MyInterceptor();
}
public class MyInterceptor implements RequestInterceptor {
@Override
public void apply(RequestTemplate template) {
String lines;
try {
lines = URLDecoder.decode(String.valueOf(template.queries().get("url")), "UTF-8");
template.queries.put("url", Collections.singletonList(lines));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
}
But I am getting UnsupportedOperationException and I believe at this point I can not modify the queries. Any suggestion is highly appreciated. (You will notice that the query parameter is leaving ':' (colon) as it is instead of encoding it to %3A).