问题
I am currently using a YAML file to generate the models and the API clients using the swagger plugin and I am using Feign OkHttpClient to make requests to the API, the problem here is the client encodes the URL but ignores the Slash(es) with this the API call fails. Is there a way to add decodeSlash parameter in the client? Or can this be achieved using an interceptor?
Here is the sample path param where I am running into this issue. QgKuK2DU/0%3D where as it should be QgKuK2DU%2F0%3D
回答1:
decodeSlash can only be set via the @RequestLine annotation. If you do not have access to the annotation you will need to replace the uri using a RequestInterceptor.
回答2:
If you use the openapi-generator you can modify the templates (also described here) yourself to add the decodeSlash Parameter:
git clone https://github.com/openapitools/openapi-generator
cd openapi-generator
git checkout v4.2.0 # The Version Tag you are actually using
cd modules/openapi-generator/src/main/resources/Java/libraries/feign/
cp api.mustache <your_local_project>/src/main/resources/Java/libraries/feign
In api.mustache change the 2 appearances of @RequestLine:
- @RequestLine("{{httpMethod}} {{{path}}}{{#hasQueryParams}}?{{/hasQueryParams}}{{#queryParams}}{{baseName}}={{=<% %>=}}{<%paramName%>}<%={{ }}=%>{{#hasMore}}&{{/hasMore}}{{/queryParams}}")
+ @RequestLine(value="{{httpMethod}} {{{path}}}{{#hasQueryParams}}?{{/hasQueryParams}}{{#queryParams}}{{baseName}}={{=<% %>=}}{<%paramName%>}<%={{ }}=%>{{#hasMore}}&{{/hasMore}}{{/queryParams}}", decodeSlash = false)
- @RequestLine("{{httpMethod}} {{{path}}}?{{#queryParams}}{{baseName}}={{=<% %>=}}{<%paramName%>}<%={{ }}=%>{{#hasMore}}&{{/hasMore}}{{/queryParams}}")
+ @RequestLine(value="{{httpMethod}} {{{path}}}?{{#queryParams}}{{baseName}}={{=<% %>=}}{<%paramName%>}<%={{ }}=%>{{#hasMore}}&{{/hasMore}}{{/queryParams}}", decodeSlash = false)
Using the openapi-generator-maven-plugin add the templateDirectory to the <configuration> block:
<templateDirectory>src/main/resources/Java/libraries/feign</templateDirectory>
来源:https://stackoverflow.com/questions/53090390/adding-decodeslash-using-feign-requestline