在了解了webservice,soap,wsdl,uddi以后那么就开始做个webservice的小例子吧,原来认为会很难的,但是做完了想想看,和大学时候老师讲的RPC是一样的了。就是一个远程调用过程不得不说So Easy~~
来吧 先配置一下我们的环境吧。Axis是apache一个开源的webservice服务,需要web容器进行发布。这里我们java烟民经常使用的是tomcat了。呵呵,太巧了。那么首先看看tomcat中需要配置什么吧(大伙都说安装版的什么都基本不用配置),呵呵。说得好,如果是免安装版的呢,我勒个去。我第一次用免安装版的时候,点击startup.bat愣是看了一个多钟头,反反复复总是一闪而过。气得我一天没吃饭。不说这没用的了。看看配置吧:
在tomcat安装路径下的bin文件夹中找到startup.bat(启动文件),在文件最上边加上一下两行:
SET JAVA_HOME=C:\Program Files\Java\jdk1.6.0_22 #jdk的安装路径
SET CATALINA_HOME=D:\apache-tomcat-6.0.32 #tomcat的安装路径
很简单吧,试试双击startup.bat,高兴吧,tomcat启动了。
接下来就是axis的配置了吧---这里假设你已经下载了axis包。并将axis项目放在tomcat的webapps目录下
在系统环境变量设置一下变量:
CATALINA_HOME
D:\apache-tomcat-6.0.32
AXIS_HOME
%CATALINA_HOME%\webapps\axis
AXIS_LIB
%AXIS_HOME%\lib
AXISCLASSPATH
%AXIS_LIB%\axis.jar;%AXIS_LIB%\commons-discovery.jar; %AXIS_LIB%\commons-logging.jar;%AXIS_LIB%\jaxrpc.jar; %AXIS_LIB%\saaj.jar;%AXIS_LIB%\log4j-1.2.8.jar; %AXIS_LIB%\xml-apis.jar;%AXIS_LIB%\xercesImpl.jar
CLASSPATH 中加入:
%AXIS_LIB%\axis.jar;%AXIS_LIB%\commons-discovery.jar; %AXIS_LIB%\commons-logging.jar;%AXIS_LIB%\jaxrpc.jar; %AXIS_LIB%\saaj.jar;%AXIS_LIB%\log4j-1.2.8.jar; %AXIS_LIB%\xml-apis.jar;%AXIS_LIB%\xercesImpl.jar
好了,下面测试一下我们的Axis吧:
启动浏览器,输入http://localhost:8080/axis/happyaxis.jsp,如果你看到如下的内容显示在网页中
Axis Happiness Page
Examining webapp configuration
Needed Components
Found SAAJ API (javax.xml.soap.SOAPMessage) at an unknown location
Found JAX-RPC API (javax.xml.rpc.Service) at D:\apache-tomcat-6.0.32\webapps\axis\WEB-INF\lib\jaxrpc.jar
Found Apache-Axis (org.apache.axis.transport.http.AxisServlet) at D:\apache-tomcat-6.0.32\webapps\axis\WEB-INF\lib\axis.jar
Found Jakarta-Commons Discovery (org.apache.commons.discovery.Resource) at D:\apache-tomcat-6.0.32\webapps\axis\WEB-INF\lib\commons-discovery.jar
Found Jakarta-Commons Logging (org.apache.commons.logging.Log) at D:\apache-tomcat-6.0.32\webapps\axis\WEB-INF\lib\commons-logging.jar
Found Log4j (org.apache.log4j.Layout) at D:\apache-tomcat-6.0.32\webapps\axis\WEB-INF\lib\log4j-1.2.8.jar
Found IBM's WSDL4Java (com.ibm.wsdl.factory.WSDLFactoryImpl) at D:\apache-tomcat-6.0.32\webapps\axis\WEB-INF\lib\wsdl4j.jar
Found JAXP implementation (javax.xml.parsers.SAXParserFactory) at an unknown location
Found Activation API (javax.activation.DataHandler) at an unknown location
Optional Components
Warning: could not find class javax.mail.internet.MimeMessage from file mail.jar
Attachments will not work
See http://java.sun.com/products/javamail/
Warning: could not find class org.apache.xml.security.Init from file xmlsec.jar
XML Security is not supported
See http://xml.apache.org/security/
Found Java Secure Socket Extension (javax.net.ssl.SSLSocketFactory) at an unknown location
The core axis libraries are present. 2 optional axis libraries are missing
Note: On Tomcat 4.x and Java1.4, you may need to put libraries that contain java.* or javax.* packages into CATALINA_HOME/common/lib
jaxrpc.jar and saaj.jar are two such libraries.
Note: Even if everything this page probes for is present, there is no guarantee your web service will work, because there are many configuration options that we do not check for. These tests are necessary but not sufficient
Examining Application Server
Servlet version 2.5
XML Parser com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl
XML ParserLocation an unknown location
Examining System Properties
java.runtime.name=Java(TM) SE Runtime Environment
sun.boot.library.path=C:\Program Files\Java\jdk1.6.0_22\jre\bin
java.vm.version=17.1-b03
shared.loader=
java.vm.vendor=Sun Microsystems Inc.
java.vendor.url=http://java.sun.com/
path.separator=;
java.vm.name=Java HotSpot(TM) Client VM
tomcat.util.buf.StringCache.byte.enabled=true
file.encoding.pkg=sun.io
java.util.logging.config.file=D:\apache-tomcat-6.0.32\conf\logging.properties
user.country=CN
sun.java.launcher=SUN_STANDARD
sun.os.patch.level=Service Pack 3
java.vm.specification.name=Java Virtual Machine Specification
user.dir=D:\apache-tomcat-6.0.32\bin
java.runtime.version=1.6.0_22-b04
java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment
java.endorsed.dirs=D:\apache-tomcat-6.0.32\endorsed
os.arch=x86
java.io.tmpdir=D:\apache-tomcat-6.0.32\temp
line.separator=
java.vm.specification.vendor=Sun Microsystems Inc.
user.variant=
java.util.logging.manager=org.apache.juli.ClassLoaderLogManager
java.naming.factory.url.pkgs=org.apache.naming
os.name=Windows XP
sun.jnu.encoding=GBK
java.library.path=C:\Program Files\Java\jdk1.6.0_22\bin;.;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;D:\PROGRA~1\Borland\CBUILD~1\Bin;D:\PROGRA~1\Borland\CBUILD~1\Projects\Bpl;C:\Program Files\PC Connectivity Solution\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Common Files\Thunder Network\KanKan\Codecs;C:\Program Files\Java\jdk1.6.0_22\bin;E:\resource\android-sdk-windows\tools
java.specification.name=Java Platform API Specification
java.class.version=50.0
sun.management.compiler=HotSpot Client Compiler
os.version=5.1
user.home=C:\Documents and Settings\Administrator
user.timezone=Asia/Shanghai
catalina.useNaming=true
java.awt.printerjob=sun.awt.windows.WPrinterJob
java.specification.version=1.6
file.encoding=GBK
catalina.home=D:\apache-tomcat-6.0.32
user.name=Administrator
java.class.path=D:\apache-tomcat-6.0.32\bin\bootstrap.jar
java.naming.factory.initial=org.apache.naming.java.javaURLContextFactory
package.definition=sun.,java.,org.apache.catalina.,org.apache.coyote.,org.apache.tomcat.,org.apache.jasper.
java.vm.specification.version=1.0
sun.arch.data.model=32
java.home=C:\Program Files\Java\jdk1.6.0_22\jre
java.specification.vendor=Sun Microsystems Inc.
user.language=zh
awt.toolkit=sun.awt.windows.WToolkit
java.vm.info=mixed mode, sharing
java.version=1.6.0_22
java.ext.dirs=C:\Program Files\Java\jdk1.6.0_22\jre\lib\ext;C:\WINDOWS\Sun\Java\lib\ext
sun.boot.class.path=C:\Program Files\Java\jdk1.6.0_22\jre\lib\resources.jar;C:\Program Files\Java\jdk1.6.0_22\jre\lib\rt.jar;C:\Program Files\Java\jdk1.6.0_22\jre\lib\sunrsasign.jar;C:\Program Files\Java\jdk1.6.0_22\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.6.0_22\jre\lib\jce.jar;C:\Program Files\Java\jdk1.6.0_22\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.6.0_22\jre\classes
java.vendor=Sun Microsystems Inc.
server.loader=
catalina.base=D:\apache-tomcat-6.0.32
file.separator=\
java.vendor.url.bug=http://java.sun.com/cgi-bin/bugreport.cgi
common.loader=${catalina.base}/lib,${catalina.base}/lib/*.jar,${catalina.home}/lib,${catalina.home}/lib/*.jar
sun.cpu.endian=little
sun.io.unicode.encoding=UnicodeLittle
package.access=sun.,org.apache.catalina.,org.apache.coyote.,org.apache.tomcat.,org.apache.jasper.,sun.beans.
sun.desktop=windows
sun.cpu.isalist=pentium_pro+mmx pentium_pro pentium+mmx pentium i486 i386 i86
Platform: Apache Tomcat/6.0.32
恭喜你,配置成功了。下边我们来写一个简单的webservice接口,并通过客户端调用试试看:
在我们的axis工程下创建一个java文件,命名为SayHello.java,文件内容如下
public class SayHello {
public String getName(String name) {
return " hello " + name;
}
}
不要笑,就是这么简单。Axis提供了两种服务发布方式,一种是即时发布(Instant Deployment),一种是定制发布(Custom Deployment)。是不是看着很别扭,我就是
想强调一下啊,我们这里使用的是第一种发布方式。这种方式简单。关于两者的是非暂时不分析。我们就是要从简单的入手了。
下好了之后访问以下http://localhost:8080/axis/SayHello.jws?wsdl ,看到了吧我们的WSDL就这么简单的直接生成了。
下面我们就在客户端写个简单的客户端访问下我们的webservice吧,SayHelloClient.java如下:
package com.jftt.soap;
import javax.xml.namespace.QName;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
public class SayHelloClient {
public static void main(String[] args) {
try {
String endpoint = " http://localhost:8080/axis/SayHello.jws " ;
Service service = new Service();
Call call = null ;
call = (Call) service.createCall();
call.setOperationName( new QName( " http://localhost:8080/axis/SayHello.jws " ,
" getName " ));
call.setTargetEndpointAddress( new java.net.URL(endpoint));
String ret = (String) call.invoke( new Object[] { " changjianlong " });
System.out.println( " return value is " + ret);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
导入缺少的包----这里用到的包在我们下载的axis包里包含的都有,可以找找看。真不行的话就将axis的lib中的包全部引入了。呵呵