用于添加 headers 的自定义拦截器不使用更新的 headers 发送消息

Custom interceptor for adding headers not sending the message with updated headers

我就是这样做的

    org.apache.cxf.endpoint.Client client = ClientProxy.getClient(port);
            client.getInInterceptors().add(new HeaderHandler());

            try {
                port.[my method](inputs);
}

我的处理程序看起来像

public class HeaderHandler extends LoggingInInterceptor {

    public HeaderHandler() {
        super(Phase.PRE_STREAM);
    }

    @SuppressWarnings("unchecked")
    @Override
    public void handleMessage(Message message) throws org.apache.cxf.interceptor.Fault {
        Map<String, List<String>> headers = CastUtils.cast((Map<String, List<String>>) message.get(Message.PROTOCOL_HEADERS));
        List<String> clientid = new ArrayList<String>();
        clientid.add("");
        headers.put("clientid", clientid);

        List<String> authorization = new ArrayList<String>();
        authorization.add("Basic @#$%#$%^#$^");
        headers.put("Authorization", authorization);

        message.put(Message.PROTOCOL_HEADERS, headers);
        System.out.println("called");
    }
}

处理消息方法总是被调用并通过调试我可以找到我在方法结束之前保存的消息中创建的headers但是当端口方法解析时我只有默认的headers再次发送 soap 请求

Headers: {Accept=[*/*], cache-control=[no-cache], connection=[keep-alive], Content-Length=[494], content-type=[application/soap+xml; action="urn:[my method]"; charset=UTF-8], host=[localhost:8080], pragma=[no-cache], user-agent=[Apache-CXF/3.1.8]}

什么给了?

请查看CXF FAQ:How can I add soap headers to the request/response?,我建议使用CXF方式

Client client = ClientProxy.getClient(port);

// Add HTTP headers to the web service request
Map<String, List<String>> headers = new HashMap<String, List<String>>();
headers.put("Authorization", Arrays.asList(authorization));
headers.put("clientid", Arrays.asList(clientid));
proxy.getRequestContext().put(Message.PROTOCOL_HEADERS, headers);

// Add SOAP headers
List<Header> soapheaders = new ArrayList<Header>();
Header dummyHeader = new Header(new QName("uri:org.apache.cxf", "dummy"), "decapitated", new JAXBDataBinding(String.class));
soapheaders.add(dummyHeader);
client.getRequestContext().put(Header.HEADER_LIST, soapheaders);