使用axis写web service

今天搞了一天ws,郁闷至极,乱七八糟的资料,不知道哪些能用,哪些不好。其实昨天已经写出了一个程序,但是跑出来,结果不懂,也不知道怎么用。

今天总算用了一点头绪。

说一下在Eclipse下面怎么用吧。

首先要配置axis和tomcat,这里不赘述了。这里用的是axis1的版本1.4

新建dynamic web project

在源文件里写一个程序:


public class Converter {
public float cf(float c){
   return c*2;
}
public float fc(float f){
   return f/2;
}

}
右键,webservice,为它生产wsdl文件。

然后在eclipse启动tomcat,在浏览器中输入:

http://localhost:8080/webservice/services/Converter?wsdl

得到如下结果

<?xml version="1.0" encoding="UTF-8" ?>
- <wsdl:definitions targetNamespace="http://DefaultNamespace" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://DefaultNamespace" xmlns:intf="http://DefaultNamespace" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <!--
WSDL created by Apache Axis version: 1.4
Built on Apr 22, 2006 (06:55:48 PDT)
--> 
- <wsdl:types>
- <schema elementFormDefault="qualified" targetNamespace="http://DefaultNamespace" xmlns="http://www.w3.org/2001/XMLSchema">
- <element name="fc">
- <complexType>
- <sequence>
<element name="f" type="xsd:float" />
</sequence>
</complexType>
</element>
- <element name="fcResponse">
+ <complexType>
- <sequence>
<element name="fcReturn" type="xsd:float" />
</sequence>
</complexType>
</element>
- <element name="cf">
- <complexType>
- <sequence>
<element name="c" type="xsd:float" />
</sequence>
</complexType>
</element>
- <element name="cfResponse">
- <complexType>
- <sequence>
<element name="cfReturn" type="xsd:float" />
</sequence>
</complexType>
</element>
</schema>
</wsdl:types>
- <wsdl:message name="cfRequest">
<wsdl:part element="impl:cf" name="parameters" />
</wsdl:message>
- <wsdl:message name="cfResponse">
<wsdl:part element="impl:cfResponse" name="parameters" />
</wsdl:message>
- <wsdl:message name="fcRequest">
<wsdl:part element="impl:fc" name="parameters" />
</wsdl:message>
- <wsdl:message name="fcResponse">
<wsdl:part element="impl:fcResponse" name="parameters" />
</wsdl:message>
- <wsdl:portType name="Converter">
- <wsdl:operation name="fc">
......

这是一个wsdl文件,关于wsdl大家可以查一下资料。简单的说它就是描述提供的服务的一个文件。这里不赘述了。关键是这一部分:

<wsdl:types>

- <schema elementFormDefault="qualified" targetNamespace="http://DefaultNamespace" xmlns="http://www.w3.org/2001/XMLSchema">
- <element name="fc">
- <complexType>
- <sequence>
-......

</wsdl:types>

描述了怎么调用这个web 服务,我们就在浏览器里使用http协议访问这个服务。使用的地址是:

http://localhost:8080/webservice/services/Converter?method=cf&c=2

http://localhost:8080/webservice/services/Converter这是服务地址

method=cf说明使用的方法是cf在上面的wsdl中有描述,而c=2是传递参数为2

得到结果是:

<?xml version="1.0" encoding="UTF-8" ?>
- <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <soapenv:Body>
- <cfResponse xmlns="">
<ns1:cfReturn xmlns:ns1="http://DefaultNamespace">4.0</ns1:cfReturn>
</cfResponse>
</soapenv:Body>

</soapenv:Envelope>

另外也可以将axis部署到tomcat下面直接编写相关的类,将其改名为jws后缀即可,tomcat会对其进行编译等工作。接下来的工作是看看如何使用ant来整合,因为部署到服务器要使用ant,如果不能用ant来部署的话纯手动就太麻烦了。

----------------------

ps:忙忙碌碌一天下来就整了这么点有用的东西。不过还好,再弄下ant的问题应该就可以写业务逻辑了。当然还可能面对axis处理复杂数据的一些问题。

over!

原文地址:https://www.cnblogs.com/macula7/p/1960412.html