Adding decodeSlash using Feign @RequestLine

瘦欲@ 提交于 2020-01-15 15:56:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!