如何将消息上下文标头添加到apache轴2 Java [英] how add Message context headers to apache axis 2 Java
问题描述
我正在研究Web服务.我想知道如何在JAX-WS类型的Web服务中向SOAP请求添加标头.
I am working on web services. I want to know how do we add headers to SOAP request in JAX-WS type web services.
考虑这样的标题.
Map<String, List<String>> headers = new HashMap<String, List<String>>();
headers.put("Username", Collections.singletonList("aaaa"));
headers.put("Password", Collections.singletonList("aaaa"));
我的客户端类中有存根对象.我正在使用Apache Axis2.所有类都是自动生成的.
I have stub object in my client class. I am using Apache Axis 2. All the classes are automatically generated.
SimpleSTub stub = new Simplestub();
我想在客户端中添加此标头信息.
I want to add this header information in client.
MessageContext.HTTP_REQUEST_HEADERS, headers
修改
在普通类中的实际实现发现为
The actual implementation in a normal class found as
私有静态最终字符串WS_URL ="http://localhost:9999/ws/hello?wsdl";
private static final String WS_URL = "http://localhost:9999/ws/hello?wsdl";
公共静态void main(String [] args)引发异常{
public static void main(String[] args) throws Exception {
URL url =新URL(WS_URL); QName qname =新的QName("http://ws.mkyong.com/","HelloWorldImplService");
URL url = new URL(WS_URL); QName qname = new QName("http://ws.mkyong.com/", "HelloWorldImplService");
Service service = Service.create(url, qname);
HelloWorld hello = service.getPort(HelloWorld.class);
/*******************UserName & Password ******************************/
Map<String, Object> req_ctx = ((BindingProvider)hello).getRequestContext();
req_ctx.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, WS_URL);
Map<String, List<String>> headers = new HashMap<String, List<String>>();
headers.put("Username", Collections.singletonList("mkyong"));
headers.put("Password", Collections.singletonList("password"));
req_ctx.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
/**********************************************************************/
System.out.println(hello.getHelloWorldAsString());
任何人都可以告诉您如何实现这一目标.
Can any one tell how to achieve this.
谢谢.
推荐答案
您正在使用现有的解决方案.达到此目的的最基本方法是
You're sort of on your way to the solution with what you already have. The most basic way to achieve this is
-
在您的客户端代码中,通过
SimpleStub
Map<String,Object> context = ((BindingProvder)stub).getRequestContext()
Map<String,List> headers = context.get(MessageContext.HTTP_REQUEST_HEADERS)
更新地图并将其填充回请求上下文对象中
Update the map and stuff it back in the request context object
context.put(MessageContext.HTTP_REQUEST_HEADERS,headers)
以上都很好.但是,如果您尝试执行我认为是添加身份验证参数的操作,则推荐的方法是
The above is all well and good. If however you're trying to do what I presume is add authentication parameters, the recommended way is
context.put(BindingProvder.USERNAME_PROPERTY,"username");
context.put(BindingProvder.PASSWORD_PROPERTY,"password");
这篇关于如何将消息上下文标头添加到apache轴2 Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!