我们可以使用 JAX-WS 上下文来自定义客户端代理的属性。特别地,上下文可以用来修改连接属性以及通过协议头发送数据。例如,我们可以用上下文添加一个SOAP头,请求消息和响应消息都可以添加。客户端支持下列类型的上下文:
要设置一个特殊的请求上下文属性 ContextPropertyName,其值为 PropertyValue 可以用下面的代码:
// Set request context property. java.util.Map<String, Object> requestContext = ((javax.xml.ws.BindingProvider)port).getRequestContext(); requestContext.put(ContextPropertyName, PropertyValue); // Invoke an operation. port.SomeOperation();
我们需要将端口对象映射为 javax.xml.ws.BindingProvider ,以此来获取请求上下文。请求上下文本身是一个 java.util.Map<String, Object> 类型,也就是一个有着String类型的“键”和任意类型的“值”的哈希表。使用 java.util.Map.put() 在哈希表中创建一个新的条目。
要检索一个特定的响应上下文属性 ContextPropertyName 使用如下代码:
// Invoke an operation. port.SomeOperation(); // Read response context property. java.util.Map<String, Object> responseContext = ((javax.xml.ws.BindingProvider)port).getResponseContext(); PropertyType propValue = (PropertyType) responseContext.get(ContextPropertyName);
响应上下文的类型是 java.util.Map<String, Object> ,也是一个有着String类型的“键”和任意类型的“值”的哈希表。我们可以使用 java.util.Map.get() 来获取响应上下文属性哈希表中的一个条目。
CXF支持下列上下文属性:
上下文属性名 | 上下文属性类型 |
---|---|
org.apache.cxf.ws.addressing.JAXWSAConstants.CLIENT_ADDRESSING_PROPERTIES | org.apache.cxf.ws.addressing.AddressingProperties |