一、搭建简单的axis web服务

简介:

1、在官方网站下载axis的工程(这个等下就有用的)和源码、jar包等,下载地址是:

http://labs.renren.com/apache-mirror//ws/axis/1_4/

2、解压下载的工程或源码(两个中任意一个都可以),解压axis-bin-1.4可以看到大致目录是这样的:

 

docs是文档、lib是jar包、sample是示例、xmls是当前工程所需的xml、webapps是当前工程的webroot目录;

我们打开webapps目录就可以看到一个axis的文件夹,这个文件夹里面有WEB-INF文件夹和一些页面,将axis复制到你的tomcat的webapps目录下。然后启动tomcat服务,访问http://localhost:8080/axis/,看到下面的解码就说明部署成功了:

 

以后我们将和这个工程不离不弃,它将在我们的axis1.x的webService中发挥很大的作用!

 

3、创建我们自己的web工程,这里我新建的AxisWebService;创建好工程后,将刚才解压的axis-bin中的lib的jar包copy到当前工程的lib中;

axis-ant.jar

axis.jar

commons-discovery-0.2.jar

commons-logging-1.0.4.jar

jaxrpc.jar

log4j-1.2.8.jar

saaj.jar

wsdl4j-1.5.1.jar

activation-1.1.jar

mail-1.4.jar

 

创建webService类文件,代码如下:

 

复制代码
 
  
package com.hoo.service;

/**
* <b>function:</b>jws的axis WebService
*
@author hoojo
* @createDate Dec 15, 2010 17:03:49 PM
* @file HelloWorldService.java
* @package com.hoo.service
* @project AxisWebService
* @blog
http://blog.csdn.net/IBM_hoojo
* @email hoojo_@126.com
*
@version 1.0
*/
public class HelloWorldService {

public String sayHello(String name, int age) {
return name + " say : hello world! [axis] my age is " + age;
}
}
复制代码

 

4、复制HelloWorldService.java到我们刚才复制的axis文件夹下即可;也就是tomcat下的webapps下的axis下即可;注意:还有重要的一般就是要将这个java文件中的包名去掉,并且将这个文件重命名为HelloWorldService.jws;如果带包名的话,请求后编译的class将会在包路径下,这样我们在全球当前jws的时候就会出现找不到class,详细的你可以到发布在tomcat下的工程看看WEB-INF目录下的jwsClass就一目了然了。

上面的工作完成后,启动tomcat服务器,访问http://localhost:8080/axis/HelloWorldService.jws

你会看到:

There is a Web Service here

Click to see the WSDL

如果你和我看到的是一样的,就证明你已经成功的部署了一个axis1.x的webService。然后我们点击下就可以看到wsdl的xml文件了,内容如下:

 

复制代码
 
  
<? xml version="1.0" encoding="UTF-8" ?>
-
< wsdl:definitions targetNamespace ="http://localhost:8080/axis/HelloWorldService.jws" xmlns:apachesoap ="http://xml.apache.org/xml-soap" xmlns:impl ="http://localhost:8080/axis/HelloWorldService.jws" xmlns:intf ="http://localhost:8080/axis/HelloWorldService.jws" xmlns:soapenc ="http://schemas.xmlsoap.org/soap/encoding/" 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:message name ="sayHelloResponse" >
< wsdl:part name ="sayHelloReturn" type ="xsd:string" />
</ wsdl:message >
-
< wsdl:message name ="sayHelloRequest" >
< wsdl:part name ="name" type ="xsd:string" />
< wsdl:part name ="age" type ="xsd:int" />
</ wsdl:message >
-
< wsdl:portType name ="HelloWorldService" >
-
< wsdl:operation name ="sayHello" parameterOrder ="name age" >
< wsdl:input message ="impl:sayHelloRequest" name ="sayHelloRequest" />
< wsdl:output message ="impl:sayHelloResponse" name ="sayHelloResponse" />
</ wsdl:operation >
</ wsdl:portType >
-
< wsdl:binding name ="HelloWorldServiceSoapBinding" type ="impl:HelloWorldService" >
< wsdlsoap:binding style ="rpc" transport ="http://schemas.xmlsoap.org/soap/http" />
-
< wsdl:operation name ="sayHello" >
< wsdlsoap:operation soapAction ="" />
-
< wsdl:input name ="sayHelloRequest" >
< wsdlsoap:body encodingStyle ="http://schemas.xmlsoap.org/soap/encoding/" namespace ="http://DefaultNamespace" use ="encoded" />
</ wsdl:input >
-
< wsdl:output name ="sayHelloResponse" >
< wsdlsoap:body encodingStyle ="http://schemas.xmlsoap.org/soap/encoding/" namespace ="http://localhost:8080/axis/HelloWorldService.jws" use ="encoded" />
</ wsdl:output >
</ wsdl:operation >
</ wsdl:binding >
-
< wsdl:service name ="HelloWorldServiceService" >
-
< wsdl:port binding ="impl:HelloWorldServiceSoapBinding" name ="HelloWorldService" >
< wsdlsoap:address location ="http://localhost:8080/axis/HelloWorldService.jws" />

</ wsdl:port >
</ wsdl:service >
</ wsdl:definitions >
复制代码

 

 

 

分析下wsdl的xml文件内容:

targetNamespace=http://localhost:8080/axis/HelloWorldService.jws 

是我们部署的webservice命名空间,也就是我们访问的webService路径。

<wsdl:message name="sayHelloResponse">

           <wsdl:part name="sayHelloReturn" type="xsd:string" />

  </wsdl:message>

是返回值的信息,sayHelloResponse代表响应,即返回值,type是返回值的类型

 

<wsdl:message name="sayHelloRequest">

           <wsdl:part name="name" type="xsd:string" />

           <wsdl:part name="age" type="xsd:int" />

  </wsdl:message>

请求方法参数信息,sayHelloRequest即请求,part是参数parameter,type是参数的类型

 

<wsdl:portType name="HelloWorldService">

            <wsdl:operation name="sayHello" parameterOrder="name age">

           <wsdl:input message="impl:sayHelloRequest" name="sayHelloRequest" />

           <wsdl:output message="impl:sayHelloResponse" name="sayHelloResponse" />

        </wsdl:operation>

  </wsdl:portType>

portType的name是当前webService的名称,operation是一个操作,即可以调用的方法。name就是方法名称了,parameterOrder是参数,input输入即传入参数,output输出即返回的值;

 

<wsdl:service name="HelloWorldServiceService">

<wsdl:port binding="impl:HelloWorldServiceSoapBinding" name="HelloWorldService">

  <wsdlsoap:address location="http://localhost:8080/axis/HelloWorldService.jws" />

  </wsdl:port>

  </wsdl:service>

webService的名称和绑定的信息,以及访问的url地址。

 

5、下面编写客户端代码

代码如下:

 

复制代码
 
  
package com.hoo.client;

import java.rmi.RemoteException;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
public class HelloWorldClient {

/**
* <b>function:</b>jws axis WebService客户端
*
@author hoojo
* @createDate 2010-12-15 下午05:10:28
*
@param args
*
@throws ServiceException
*
@throws RemoteException
*/
public static void main(String[] args) throws ServiceException, RemoteException {
// webService访问地址
// String url = " http://localhost :8080/axis/HelloWorldService.jws";
String url = " http://localhost:8080/AxisWebService/HelloWorldService.jws " ;
// 创建服务
Service service = new Service();
// 创建调用句柄
Call call = (Call) service.createCall();
// 设置请求地址
call.setTargetEndpointAddress(url);
/**
* 设置调用的方法和方法的命名空间;
* 因为这里是手动发布到webroot目录下的,所以命名空间和请求地址一致
* 当然null也可以,因为本身它就没有设置命名空间,一般方法的命名空间是
* 包名倒写组成,如com.hoo.service,ns=
http://service.hoo.com
*/
call.setOperationName(
new QName( null , " sayHello " ));
/**
* 用call调用sayHello方法,设置请求的参数,返回的就是返回值了
*/
String result
= (String) call.invoke( new Object[] { " jack " , 99 });
System.out.println(result);
}
}
复制代码

 

 

分析上面的代码

url是根据xml文件中的wsdlsoap:address location的信息得到的,命名空间和方法名称是根据

 

复制代码
 
  
< wsdl:operation name ="sayHello" >
< wsdlsoap:operation soapAction ="" />
-
< wsdl:input name ="sayHelloRequest" >
< wsdlsoap:body encodingStyle ="http://schemas.xmlsoap.org/soap/encoding/" namespace ="http://DefaultNamespace" use ="encoded" />
</ wsdl:input >
-
< wsdl:output name ="sayHelloResponse" >
< wsdlsoap:body encodingStyle ="http://schemas.xmlsoap.org/soap/encoding/" namespace ="http://localhost:8080/axis/HelloWorldJWS.jws" use ="encoded" />
</ wsdl:output >
复制代码

 

 

的信息得到的,而请求参数和返回值的详细信息是在

 

复制代码
 
  
< wsdl:message name ="sayHelloRequest" >
< wsdl:part name ="name" type ="xsd:string" />
< wsdl:part name ="age" type ="xsd:int" />
</ wsdl:message >
-
< wsdl:message name ="sayHelloResponse" >
< wsdl:part name ="sayHelloReturn" type ="xsd:string" />
</ wsdl:message >
-
< wsdl:portType name ="HelloWorldJWS" >
-
< wsdl:operation name ="sayHello" parameterOrder ="name age" >
< wsdl:input message ="impl:sayHelloRequest" name ="sayHelloRequest" />
< wsdl:output message ="impl:sayHelloResponse" name ="sayHelloResponse" />
</ wsdl:operation >
</ wsdl:portType >
复制代码

 

 

里可以很详细的看到。

至于代码的call.invoke是java中反射机制,不懂的建议看看jdk文档java.lang.reflect包下的内容。

运行上面的代码就可以看到控制台输出:

jack say : hello world! [axis] my age is 99

 

好了,axis的就完成了,下面我们不用官方的axis的工程,我们写一个自己的AxisWebService工程,然后发布的tomcat的webapps中看看。

 

6、刚才copy了lib下的jar包,现在要copy下web.xml中的内容,去掉里面的AdminServlet这个配置,其他的都可保留。

然后像刚才一样,将HelloWorldService.java复制到webroot目录下,去掉包名,并且修改后缀为HelloWorldService.jws即可。(如果有兴趣可以看看,发布在tomcat目录下的当前工程的web-inf目录,看看里面是否多了些东西)最后发布当前web工程,访问http://localhost:8080/AxisWebService/HelloWorldService.jws,如果看到和刚才一样的界面,证明你快成功了。点击链接看到wsdl的xml就成功了。

好了,还没有完。看看web.xml中的配置,你大概就知道为什么了。

web.xml中最主要的文件就是org.apache.axis.transport.http.AxisServlet,它就是webService的中央控制器;即配置jws的后缀也在web.xml中定义的,在看看还有services/*,这就表明上面的访问路径也可以是这样的:http://localhost:8080/AxisWebService/services/HelloWorldService

当然如果要这样写就需要用wsdd的发布方式,详细请看下文!






本文转自hoojo博客园博客,原文链接:http://www.cnblogs.com/hoojo/archive/2010/12/20/1911357.html,如需转载请自行联系原作者
目录
相关文章
|
5天前
|
开发框架 监控 .NET
Visual Basic的Web服务和REST API开发指南
【4月更文挑战第27天】本文探讨了使用Visual Basic(VB.NET)构建Web服务和RESTful API的方法。首先介绍了Web服务的基础和REST API的概念,然后阐述了.NET Framework与.NET Core/.NET 5+对VB.NET的支持,以及ASP.NET Core在Web开发中的作用。接着,详细讲解了创建RESTful API的步骤,包括控制器与路由设置、模型绑定与验证,以及返回响应。此外,还讨论了安全措施、测试方法、部署选项和监控策略。最后强调,VB.NET开发者可以通过ASP.NET Core涉足现代Web服务开发,拓宽技术领域。
|
7天前
|
应用服务中间件 网络安全 nginx
快速上手!使用Docker和Nginx部署Web服务的完美指南
快速上手!使用Docker和Nginx部署Web服务的完美指南
|
2月前
|
网络协议 Java Nacos
nacos常见问题之在web界面 上下线服务时报错 400如何解决
Nacos是阿里云开源的服务发现和配置管理平台,用于构建动态微服务应用架构;本汇总针对Nacos在实际应用中用户常遇到的问题进行了归纳和解答,旨在帮助开发者和运维人员高效解决使用Nacos时的各类疑难杂症。
34 0
|
2月前
|
监控 Serverless 测试技术
Serverless 应用引擎常见问题之做的web服务计费如何解决
Serverless 应用引擎(Serverless Application Engine, SAE)是一种完全托管的应用平台,它允许开发者无需管理服务器即可构建和部署应用。以下是Serverless 应用引擎使用过程中的一些常见问题及其答案的汇总:
408 3
|
2月前
|
负载均衡 Java 中间件
使用Go语言构建高性能Web服务
Go语言作为一种快速、高效的编程语言,其在构建高性能Web服务方面具有独特优势。本文将探讨如何利用Go语言开发和优化Web服务,以实现更高的性能和可伸缩性。
|
3月前
|
Arthas 监控 NoSQL
web服务性能监控方案
web服务性能监控方案
|
2天前
|
缓存 监控 测试技术
【Go语言专栏】使用Go语言构建高性能Web服务
【4月更文挑战第30天】本文探讨了使用Go语言构建高性能Web服务的策略,包括Go语言在并发处理和内存管理上的优势、基本原则(如保持简单、缓存和并发控制)、标准库与第三方框架的选择、编写高效的HTTP处理器、数据库优化以及性能测试和监控。通过遵循最佳实践,开发者可以充分利用Go语言的特性,构建出高性能的Web服务。
|
9天前
|
监控 Shell
Shell脚本监控WEB服务是否正常
Shell脚本监控WEB服务是否正常
|
14天前
|
JSON API 数据库
解释如何在 Python 中实现 Web 服务(RESTful API)。
在Python中实现Web服务(RESTful API)涉及选择框架(如Flask、Django、FastAPI),定义路由及处理函数(对应HTTP请求方法),处理请求,构建响应和启动服务器。以下是一个使用Flask的简单示例:定义用户列表,通过`@app.route`装饰器设置GET和POST请求处理函数,返回JSON响应,并用`app.run()`启动服务器。实际API会包含更复杂的逻辑和错误处理。
14 1
|
1月前
|
数据采集 Java API
python并发编程: Python使用线程池在Web服务中实现加速
python并发编程: Python使用线程池在Web服务中实现加速
18 3
python并发编程: Python使用线程池在Web服务中实现加速