unable to add "literal" header values on template
I am trying to use a feign.RequestInterceptor to add headers on the fly, these headers values are simple json. Example :
public void apply(RequestTemplate template) {
template.header("dummy", "{\"A\":{\"B\":\"C\"}}");
}
With version 10.7.4 I have got :
java.util.regex.PatternSyntaxException: Illegal repetition
{"B"
at java.base/java.util.regex.Pattern.error(Pattern.java:2015)
at java.base/java.util.regex.Pattern.closure(Pattern.java:3308)
at java.base/java.util.regex.Pattern.sequence(Pattern.java:2201)
at java.base/java.util.regex.Pattern.expr(Pattern.java:2056)
at java.base/java.util.regex.Pattern.compile(Pattern.java:1778)
at java.base/java.util.regex.Pattern.<init>(Pattern.java:1427)
at java.base/java.util.regex.Pattern.compile(Pattern.java:1068)
at feign.template.Expression.lambda$new$0(Expression.java:35)
at java.base/java.util.Optional.ifPresent(Optional.java:183)
at feign.template.Expression.<init>(Expression.java:35)
at feign.template.Expressions$SimpleExpression.<init>(Expressions.java:106)
at feign.template.Expressions.create(Expressions.java:86)
at feign.template.Template.parseFragment(Template.java:194)
at feign.template.Template.parseTemplate(Template.java:178)
at feign.template.Template.<init>(Template.java:60)
at feign.template.HeaderTemplate.<init>(HeaderTemplate.java:81)
at feign.template.HeaderTemplate.create(HeaderTemplate.java:57)
at feign.RequestTemplate.lambda$appendHeader$3(RequestTemplate.java:742)
at java.base/java.util.Map.compute(Map.java:1171)
at feign.RequestTemplate.appendHeader(RequestTemplate.java:740)
at feign.RequestTemplate.header(RequestTemplate.java:710)
at feign.RequestTemplate.header(RequestTemplate.java:692)
After seeing GH-1172 I had some hopes and tried with version 10.10.1, but got the same error.
I want to add "raw" headers values, no templated/parsed ones. One simple solution would just to let this method public on RequestTemplate :
private RequestTemplate header(String name, TemplateChunk... chunks)
Your literal is in the format of an expression. To use this you must pct-encode the start and end braces using %7B and %7D% to bypass expression handling. See https://tools.ietf.org/html/rfc6570#section-2.2 for more information on the template expression format and why what you are attempting is not supported without encoding.
@kdavisk6 thanks very much for your kindly explanation. But how to decode the "%7B" and "%7D" back to "{" and "}" ? I found in BodyTemplate.expand(), "%7B" and "%7D" will be decoded back to "{" and "}" . But I didn't find a similar code in HeaderTemplate.java
@dumaswong
At the moment, you are correct. HeaderTemplate does not have any built in JSON support. My recommendation is to use an expression and pass in the JSON value as the result:
template.header("dummy", "{json}");
template.result(Map.of("dummy", "{\"A\":{\"B\":\"C\"}}"));
This will produce the result you expect. Here is an example using the annotations:
public interface Example {
@Request("/")
@Headers("X-JSON: {json}")
public void get(@Param("json") String json);
}
In my situation, the value of header comes from theadlocal. It seem not suitable to pass in parameter. Is there a way to fill customized value to varBuilder in ReflectiveFeign.create(Object[]) which is used in RequestTemplate.resolve(Map<String, ?>)
@dumaswong
At the moment, you are correct.
HeaderTemplatedoes not have any built in JSON support. My recommendation is to use an expression and pass in the JSON value as the result:template.header("dummy", "{json}"); template.result(Map.of("dummy", "{\"A\":{\"B\":\"C\"}}"));This will produce the result you expect. Here is an example using the annotations:
public interface Example { @Request("/") @Headers("X-JSON: {json}") public void get(@Param("json") String json); }
I tried this suggestion. It works great with version 10.7.0. I needed to upgrade my version of spring boot to 2.5.8 wich required spring-cloud-starter-openfeign version 3.1.0. This version embed feign core version 11.7 and my project doesnt work anymore.
I did some test and with feign core version 11.0 my project works. With version 11.1 it doesnt work. There might be an error regarding next version.
Hi,
Any news on this issue. As I mentionned in my previous post, I needed to hard coded version 11.0 in my pom to make it work in my project. For information, this raises a warning in my project,
WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by feign.DefaultMethodHandler (file:/D:/REPO/P/.m2/repository/io/github/openfeign/feign-core/11.0/feign-core-11.0.jar) to field java.lang.invoke.MethodHandles$Lookup.IMPL_LOOKUP WARNING: Please consider reporting this to the maintainers of feign.DefaultMethodHandler WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations WARNING: All illegal access operations will be denied in a future release
Is it possible to looke for a fix in latest version?
thanks
Hi,
Any news on this issue. As I mentionned in my previous post, I needed to hard coded version 11.0 in my pom to make it work in my project. For information, this raises a warning in my project,
WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by feign.DefaultMethodHandler (file:/D:/REPO/P/.m2/repository/io/github/openfeign/feign-core/11.0/feign-core-11.0.jar) to field java.lang.invoke.MethodHandles$Lookup.IMPL_LOOKUP WARNING: Please consider reporting this to the maintainers of feign.DefaultMethodHandler WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations WARNING: All illegal access operations will be denied in a future releaseIs it possible to looke for a fix in latest version?
thanks
Hi,boy , Is there any latest solution ?
This appears to be fixed in master at HEAD now but not yet released.
I confirmed by writing a unit test for this case -- it passes. I checked out the last release and the unit test failed there.
The fix for this is related to the fixes that came in for https://github.com/OpenFeign/feign/issues/1464
@kdavisk6 @velo I believe this can be closed once the next release goes out. I opened https://github.com/OpenFeign/feign/pull/1800 to cover this case with a test if you want it.
I meet same problem, and I will show my resolution.
copy file (HeaderTemplate and RequestTemplate) from new version of this commit to your project in same package position, it will overwrite the version of openfeign in jar.
then change the HeaderTemplate's appendHeader() method to public.
finally, to add header in this way:
requestTemplate.appendHeader("someJson", "{\"name\":\"...+++AAA\"}", true);
It's the only way I can find to resolve the problem but not upgrade feign version. If you have a better way, please tell me :)