Currently Encoder is applied only for @RequestBody params. What about @RequestParam?
I know this is special case but we have lot of working legacy code. For current solution this objects are serialized using Jackson (we use @JsonValue) but Feign uses simple toString().
Possible solutions:
Encoder for all parameter typesIf you use @FeignClient and Spring Cloud Netflix you can use org.springframework.cloud.netflix.feign.FeignFormatterRegistrar.
You should create a bean in client's configuration:
@Bean
public FeignFormatterRegistrar localDateFeignFormatterRegistrar() {
return new FeignFormatterRegistrar() {
@Override
public void registerFormatters(FormatterRegistry formatterRegistry) {
//Here should be some code.
}
};
}
In registerFormatters() method you have access to object of FormatterRegistry.
If you look inside the FormatterRegistry class, you will see a method:
void addFormatterForFieldType(Class<?> fieldType, Printer<?> printer, Parser<?> parser);
This is a method that will let you to associate specific class with a printer (for building a string from an object) and parser (for building an object from a string).
FeignFormatterRegistrar has some minor side-effect when encoding object and its fields for example Pageable. Using encoder we can encode to &size=10&page=1 but using FeignFormatterRegistrar we can encode only to &pageable=true&size=10&page=1 which introduces unnecessary url parameter pageable=true.
This is not the forum to discuss FeignFormatterRegistrar and other spring cloud Netflix things
Using JSON for request params seems like a bad design to me. However, since you need to... Why doesn't @Param(expander = MyJsonTextExpander.class) work for you?
Since this issue is related primarily to Spring, I'm going to close this. If this comes up again and is related to Param.Expander, I'll reconsider.
Most helpful comment
This is not the forum to discuss FeignFormatterRegistrar and other spring cloud Netflix things