PatternSyntaxException: Illegal character
I want to set the requestTemplate header value to an array, but an error occurs
public class FeignConfig implements RequestInterceptor {
@Override
public void apply(RequestTemplate requestTemplate) {
requestTemplate.header("roles", "{\"roles\":[\"default-role\"]}");
}
}
}
java.util.regex.PatternSyntaxException: Illegal character range near index 10
["default-role"]
^
at java.util.regex.Pattern.error(Pattern.java:1957)
at java.util.regex.Pattern.range(Pattern.java:2657)
at java.util.regex.Pattern.clazz(Pattern.java:2564)
at java.util.regex.Pattern.sequence(Pattern.java:2065)
at java.util.regex.Pattern.expr(Pattern.java:1998)
at java.util.regex.Pattern.compile(Pattern.java:1698)
at java.util.regex.Pattern.<init>(Pattern.java:1351)
at java.util.regex.Pattern.compile(Pattern.java:1028)
at feign.template.Expression.lambda$new$0(Expression.java:35)
at java.util.Optional.ifPresent(Optional.java:159)
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:218)
at feign.template.Template.parseTemplate(Template.java:202)
at feign.template.Template.<init>(Template.java:61)
at feign.template.HeaderTemplate.<init>(HeaderTemplate.java:110)
at feign.template.HeaderTemplate.create(HeaderTemplate.java:72)
at feign.RequestTemplate.lambda$appendHeader$3(RequestTemplate.java:758)
at java.util.Map.compute(Map.java:1093)
at feign.RequestTemplate.appendHeader(RequestTemplate.java:756)
at feign.RequestTemplate.header(RequestTemplate.java:726)
at feign.RequestTemplate.header(RequestTemplate.java:694)
at com.commercial.consumer.config.FeignConfig.apply(FeignConfig.java:20)
......
How can I set it up so that it can work ?
@myifeng I believe you can try with - eg: public class FeignConfig implements RequestInterceptor { @Override public void apply(RequestTemplate requestTemplate) { requestTemplate.header("roles", "{"roles":["default-role"]}"); } } }
@guptnis Thank you very much, but your answer does not seem to solve my problem.
"{\"roles\":[\"default-role\"]}" from JSONObject.toJSONString(user)
I am also facing the same issue with feign and setting header value that contains JSON array object. Any help would be highly appreciated !!.
I just discovered that this stopped working in v10.7.2 and later versions. So, It seems that this issue is happening because of the following commits.
https://github.com/OpenFeign/feign/pull/1138/commits https://github.com/OpenFeign/feign/pull/1139/commits/791f9e52d3cfcf71bf4e58dc121f2d44672374c5
I found a solution: encode the value to prevent { , because feign treats a string beginning with { as an expression or a literal.
requestTemplate.header("roles", "{\"roles\":[\"default-role\"]}"); // It not work.
requestTemplate.header("roles", URLEncoder.encode("{\"roles\":[\"default-role\"]}"), StandardCharsets.UTF_8.name()); // It work.
But I think this is unfriendly to developers.
@myifeng - This will also require decoding the header value at receiving end. So, I don't think this is the best approach.
@authors - Can you please look into this issue as its impacting our ability to use feign client for REST calls.
Hi All - Is there any update on this issue? I am sure this is breaking for many consumer of feign library.
Hi @mr-rajeshrathod , feign is mainly run by volunteers, so if that is impacting you, feel free to raise a PR and I will review for you.
Here's another issue this looks like another json header issue fixed in master at HEAD now. Related to the fixes that came in for https://github.com/OpenFeign/feign/issues/1464
I believe this can be closed @velo