在Java中使用Web服务时如何触发处理程序类 [英] How to trigger a handler class when consuming web-service in java

查看:147
本文介绍了在Java中使用Web服务时如何触发处理程序类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Axis 1.4我构建了将使用外部服务器服务的客户端应用程序.

带有肥皂消息的服务器应用程序响应,其中包括标头标签和正文标签.

我的标头标签有问题,我想设法找到header元素.

到目前为止已完成的操作:

我发现我需要使用使用该类扩展BasicHandler的处理程序,才能获得标头标记.来源:在Axis中处理SOAP标头

但是在使用Web服务时如何使此处理程序正常工作?我的意思是,当我从服务器收到响应以获取其header时,如何调用此处理程序.

一些博客建议我需要使用.wsdd文件.我在Weblogic 10.3.6环境中使用Jdeveloper 11g,在该环境中我仅知道用于配置的web.xml文件.

问题:如何链接这些信息(处理程序类,.wsdd文件和web.xml)以收集并使处理程序工作以获取标头标签?

解决方案

最好的开始是查看Axis指南:《 Apache轴参考指南》 ,其中将概述工作流程.

要配置从客户端触发的处理程序,您需要执行以下操作:

1-基本创建类似于以下内容的处理程序类:

package mypackge;

import javax.xml.soap.SOAPException;
import org.apache.axis.AxisFault;
import org.apache.axis.MessageContext;
import org.apache.axis.handlers.BasicHandler;
import org.apache.axis.message.SOAPHeader;
import org.apache.axis.message.SOAPHeaderElement;

public class SoapHeaderConsumerHandler
  extends BasicHandler
{
  public void invoke(MessageContext messageContext)
    throws AxisFault
  {
    // Your logic for request or response handling goes here. 
    // Basically you need to make use of the parameter
   // messageContext where you can access the soap header and body tags.
  }
}

2-创建client-config.wsdd文件.它将如下所示:

<deployment
    xmlns="http://xml.apache.org/axis/wsdd/"
    xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
    <globalConfiguration>
        <responseFlow>
            <handler name="log" type="java:mypackge.SoapHeaderConsumerHandler"/>
        </responseFlow>
    </globalConfiguration>
    <transport name="http" pivot="java:org.apache.axis.transport.http.HTTPSender"/>
</deployment>

您可以看到我仅对来自服务器端的传入响应使用处理程序.因此,无论何时客户端应用程序收到服务器的响应,处理程序类SoapHeaderConsumerHandler都会被触发,默认情况下将调用方法调用.

注意::如果要在将传出请求发送到服务器之前访问传出请求,则需要为<requestFlow>添加额外标签以添加请求处理程序.

检查来自Axis指南的Deployment(WSDD)参考:

3- client-config.wsdd文件的放置位置?

您应该将.wsdd文件放置在工作目录中.您可以使用:

轻松找到工作目录的位置

System.out.println("Working Directory = " + System.getProperty("user.dir"));

来源: 在以下位置获取当前工作目录Java

更新:

我发现没有必要将client-config.wsdd文件放在工作目录中.您可以在代码中指定此文件的路径,如下所示:

System.setProperty("axis.ClientConfigFile", "[Path goes here]\\client-config.wsdd");

您只需要将.wsdd文件放在此处.

其他有用链接:

V轴处理程序这是服务器端的示例处理程序.

在Axis中处理SOAP标头

Using Axis 1.4 I built client application that will consume external server services.

The server application response with soap message that include header tag along with body tag.

My problem with the header tag, I am trying to find away to get the header element.

What is done so far:

I found that I need to use a handler that extends BasicHandler using this class I can get the header tag. source: Dealing with SOAP Headers in Axis

But how to make this handler work when consuming web-service? I mean how to invoke this handler when ever I am receiving response from server to get its header.

Some blogs suggest I need to use .wsdd file. I am using Jdeveloper 11g with weblogic 10.3.6 environment where I am only aware of web.xml file for configuration.

Question: How to link those information(handler class, .wsdd file and web.xml) to gather and make the handler works to get the header tags?

解决方案

The best start was to check Axis guide on: Apache-Axis Reference Guide where you will have an overview of the work flow.

To configure handlers to be trigger from the client side you need to do the following:

1- Create handler class basically something similar to the following:

package mypackge;

import javax.xml.soap.SOAPException;
import org.apache.axis.AxisFault;
import org.apache.axis.MessageContext;
import org.apache.axis.handlers.BasicHandler;
import org.apache.axis.message.SOAPHeader;
import org.apache.axis.message.SOAPHeaderElement;

public class SoapHeaderConsumerHandler
  extends BasicHandler
{
  public void invoke(MessageContext messageContext)
    throws AxisFault
  {
    // Your logic for request or response handling goes here. 
    // Basically you need to make use of the parameter
   // messageContext where you can access the soap header and body tags.
  }
}

2- Create the client-config.wsdd file. it will look like the following:

<deployment
    xmlns="http://xml.apache.org/axis/wsdd/"
    xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
    <globalConfiguration>
        <responseFlow>
            <handler name="log" type="java:mypackge.SoapHeaderConsumerHandler"/>
        </responseFlow>
    </globalConfiguration>
    <transport name="http" pivot="java:org.apache.axis.transport.http.HTTPSender"/>
</deployment>

You can see that I am using only handlers for the incoming response from the server side. So when ever the client application receive a response from the server the handler class SoapHeaderConsumerHandler will be triggered and the method invoke will be called by default.

Note: if you want to access the outgoing request before send it to the server you need to add extra tag for <requestFlow> to add request handler.

Check Deployment(WSDD) Reference from Axis guide:

3- Where to place the client-config.wsdd file ?

You should place the .wsdd file in the working directory. You can easily find out the working directory location using :

System.out.println("Working Directory = " + System.getProperty("user.dir"));

Source: Getting the Current Working Directory in Java

UPDATE:

I found out that it is not necessary to put the client-config.wsdd file in the working directory. You can specify the path of this file in your code as follow:

System.setProperty("axis.ClientConfigFile", "[Path goes here]\\client-config.wsdd");

You just need to place the .wsdd file there.

Extra Useful Links:

Where to place the client-config.wsdd file in Railo

V Axis handler This is an example for the server side handlers.

Dealing with SOAP Headers in Axis

这篇关于在Java中使用Web服务时如何触发处理程序类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆