MENU

记一次Feign调用出错

November 9, 2021 • Read: 852 • 默认·无类

服务方出现Invalid character found in method name. HTTP method names must be tokens
本地有配置重写Feign的url方式,同时透传了header,导致服务方报错。
解决办法:

    @Override
    public void apply(RequestTemplate template) {
        List<String> headNameList = Arrays.asList("content-type", "content-length");
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();
        if (attributes != null) {
            HttpServletRequest request = attributes.getRequest();
            Enumeration<String> headerNames = request.getHeaderNames();
            if (headerNames != null) {
                while (headerNames.hasMoreElements()) {
                    String name = headerNames.nextElement();
                    String values = request.getHeader(name);
                    // 如果透传了原来的content-length会导致长度不一致,从而服务提供方出现
                    // Invalid character found in method name. HTTP method names must be tokens
                    if (!headNameList.contains(name)) {
                        template.header(name, values);
                    }
                }
            }
        }
    }

参考来源:http://blog.amoyiki.com/post/Spring-Cloud-4-Feign-slow.html


本文由 ONE 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
如有版权疑问交流,请给我留言:oneisall8955@gmail.com
本文永久链接:https://liuzhicong.cn/index.php/default/40.html

Archives QR Code Tip
QR Code for this page
Tipping QR Code