访问网络服务 [英] accessing web service

查看:33
本文介绍了访问网络服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建并注册了一个网络服务.现在我想制作一个简单的网络应用程序来访问网络服务.我使用axis2 作为Web 服务服务器.以xml格式文件(data.xml)的形式向客户端提供Web服务要处理的数据.我的jsp 表单有一个文本框(提供data.xml 的位置)和一个调用"按钮,可以访问调用Web 服务.我假设 jsp 首先将请求发送到 servlet,然后 servlet 将负责调用 Web 服务.Web 服务托管在 PC1 中,包含访问 Web 服务的 JSP 的 Web 应用程序在 PC2 中,data.xml 在 PC3 中.因此,用户将在 PC3 上工作,在 PC3 中打开 Web 浏览器访问 PC2 中的 jsp,然后调用 PC1 中的 Web 服务.

I have created and registered a web service. Now I wish to make a simple web app to access the web service. I used axis2 for the web service server. the client is provided with the data to be processed by the web service in the form of xml format file (data.xml). My jsp form has a textbox (to provide the location of the data.xml) and an "invoke" button that will access call the web service. I assume the jsp will first send the request to servlet and then the servlet will be the one responsible to call the web service. the web service is hosted in PC1, the web app that contains JSP accessing the web service is in PC2, and the data.xml is in PC3. So the user will be working from PC3, open web browser in PC3 to access jsp in PC2 which will then invoke the web service in PC1.

  1. 从 servlet 调用 Web 服务的语法是什么?我想知道axis2是否可以在客户端使用.
  2. 如何构造 SOAP 请求(我假设 SOAP 请求是在 servlet 中构造的)?希望我不需要按字符串构造 SOAP 请求字符串.
  3. 关于data.xml,我假设jsp将文件上传到servlet,因此servlet将接收一个文件作为参数.这是正确的方法吗?谢谢

推荐答案

由于您使用的是 Axis2,因此请使用它生成客户端代理(wsdl2java 是您最好的朋友),并将为您的 Web 服务创建一个标准的 Java 代理.无需手动创建 SOAP 信封...(至少在 99% 的情况下).

Since you're using Axis2, generate the client proxy with it (wsdl2java is your best friend) and will create a standard java proxy for your web service. There's no need to get hands dirty creating the SOAP envelope manually... (at least in 99% of the scenarios).

只需生成一个 WSDL(编写它或使用 java2wsdl 生成它),然后使用它生成一个用于调用您的 Web 服务的 Java 代理.

Just produce a WSDL (writing it or generating it with java2wsdl) then use it to produce a java proxy for calling your Web Service.

关于如何使用代理...

Regarding how to use the proxy...

恕我直言,来自jsp的直接调用是禁忌.最好让 JSP 收集数据并将它们 POST 到 servlet,然后在 servlet 内执行 WebService 调用,管理响应(如果您使用 Axis2 框架,它将是一个纯 Java 对象),处理错误,然后转发到另一个 JSP 给出结果.

IMHO direct call from jsp is a no-no. Better have the JSP collect data and POST them to a servlet, then perform WebService call within the servlet, manage response (which will be a pure java object if you use the Axis2 framework), handle errors and then forward to another JSP giving the results.

JSP 编写起来有些容易,但从长远来看,最好将它们用于计划中并将业务逻辑保留在 servlet 中

JSP are somewhat easy to write, but in the long run, better use them for what they were planned and keep business logic within servlets

这里有两个用于执行 WSDL 和客户端生成的 ant 任务.

Here two ant tasks for doing WSDL and Client Generation.

<!-- create the wsdl file -->
<target name="genwsdl">
    <echo message="Generate the WSDL file"/>
    <taskdef name="java2wsdl" classname="org.apache.ws.java2wsdl.Java2WSDLTask" classpathref="axis2.classpath"/>
    <java2wsdl className="org.stackoverflow.YourWebServiceClassName"
               servicename="YourServiceName"
               description="Whatever..."
               outputLocation="./wsdl"
               locationuri="http://localhost:8081/NameMe/CoolService">
            <classpath>
                <pathelement path="${axis2.classpath}"/>
                <pathelement location="./build/classes"/>
            </classpath>
    </java2wsdl>
    <echo message="Generation of the WSDL file complete"/>
</target>


<target name="genclient" depends="genwsdl">
    <taskdef name="axis2-wsdl2java" classname="org.apache.axis2.tool.ant.AntCodegenTask" classpathref="axis2.classpath"/>
    <axis2-wsdl2java wsdlfilename="./wsdl/YourWsdl.wsdl" output="./client overwrite="true"/>
</target>

生成客户端类(代理)后,您可以使用它们来调用 Web 服务.

Once you've generated your client classes (Proxy), you can use them to call the web service.

它将类似于:

YourServiceStub stub = new YourServiceStub(wsEndPointUrl);

然后在存根中,您将为每个远程发布的方法和包装的请求/响应对象的静态类提供一个方法.但此时必须阅读一点 Axis2 客户端文档.

then in the stub you'll have a method for each remote published method and static classes for wrapped requests/responses objects. But at this point a little reading of Axis2 client docs is mandatory.

这篇关于访问网络服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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