0

I'm using feign in spring-cloud, I have got a problem.

This is my feign client def.

@FeignClient("food-service")
public interface FoodService {
    @RequestMapping(value = {"/food"},method = {RequestMethod.GET})
    List<Food> find(@RequestParam("name") String name);
}
foodService.find("{co%%");

this call will be returned status code 400 .

Then I review the code, and I found this code in RequestTemplate class :

private String encodeIfNotVariable(String in) {
    if (in == null || in.indexOf('{') == 0) {
        return in;
    }
    return urlEncode(in);
}

The method encodeIfNotVariable called in query(String name, String... values) .

This means if the value containing { and in the first , the value cannot to be encode.

How can I fix this?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Dreampie
  • 1,321
  • 4
  • 17
  • 32

1 Answers1

0

The character { is an unsafe character so you must encode it:

Characters can be unsafe for a number of reasons. ....

.... [missing part]....

Other characters are unsafe because gateways and other transport agents are known to sometimes modify such characters. These characters are "{", "}", "|", "\", "^", "~",
"[", "]", and "`".

All unsafe characters must always be encoded within a URL. For example, the character "#" must be encoded within URLs even in systems that do not normally deal with fragment or anchor identifiers, so that if the URL is copied into another system that does use them, it will not be necessary to change the URL encoding.

In summary, character { must be encoded as %7B:

foodService.find("%7Bco%%");

More info:

Otherwise, feign-netflix uses { and } to put variables and then replace it by arguments params annotated with @Param("name"), but you are using spring impl and it is do it for you if it is needed.

Pau
  • 14,917
  • 14
  • 67
  • 94