如何将消息上下文标头添加到 apache 轴 2 Java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14233714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 15:32:58  来源:igfitidea点击:

how add Message context headers to apache axis 2 Java

javaweb-servicesjax-wsaxis2webservice-client

提问by Patan

I am working on web services. I want to know how do we add headers to SOAP request in JAX-WS type web services.

我正在研究网络服务。我想知道我们如何在 JAX-WS 类型的 Web 服务中向 SOAP 请求添加标头。

Consider My header like this.

像这样考虑我的标题。

    Map<String, List<String>> headers = new HashMap<String, List<String>>();
    headers.put("Username", Collections.singletonList("aaaa"));
    headers.put("Password", Collections.singletonList("aaaa"));

I have stub object in my client class. I am using Apache Axis 2. All the classes are automatically generated.

我的客户端类中有存根对象。我正在使用 Apache Axis 2。所有类都是自动生成的。

SimpleSTub stub = new Simplestub();

I want to add this header information in client.

我想在客户端添加这个头信息。

MessageContext.HTTP_REQUEST_HEADERS, headers

Edit

编辑

The actual implementation in a normal class found as

普通类中的实际实现发现为

private static final String WS_URL = "http://localhost:9999/ws/hello?wsdl";

private static final String WS_URL = "http://localhost:9999/ws/hello?wsdl";

public static void main(String[] args) throws Exception {

public static void main(String[] args) 抛出异常 {

URL url = new URL(WS_URL); QName qname = new QName("http://ws.mkyong.com/", "HelloWorldImplService");

URL url = 新 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.

任何人都可以告诉如何实现这一目标。

Thanks.

谢谢。

回答by kolossus

You're sort of on your way to the solution with what you already have. The most basic way to achieve this is

您正在使用已有的解决方案寻求解决方案。实现这一目标的最基本方法是

  1. Within your client code, obtain a reference to the MessageContextthrough the BindingProvideron your SimpleStub

    Map<String,Object> context = ((BindingProvder)stub).getRequestContext()
    Map<String,List> headers = context.get(MessageContext.HTTP_REQUEST_HEADERS)
    
  2. 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");   
    
  1. 在您的客户端代码中,MessageContext通过BindingProvider您的SimpleStub

    Map<String,Object> context = ((BindingProvder)stub).getRequestContext()
    Map<String,List> headers = context.get(MessageContext.HTTP_REQUEST_HEADERS)
    
  2. 更新地图并将其塞回请求上下文对象中

    context.put(MessageContext.HTTP_REQUEST_HEADERS,headers)
    

    以上一切都很好。但是,如果您尝试执行我认为是添加身份验证参数的操作,则推荐的方法是

    context.put(BindingProvder.USERNAME_PROPERTY,"username");
    context.put(BindingProvder.PASSWORD_PROPERTY,"password");