最近准备考试:将webservice 充充电,本想配置一遍,在网上资料,资源虽多,没有几个能成功的,还没有几个有原代码的,可能是自己理解能力有限,摸索了一段时间,终于调通了webservice !
在此为想学习webservice同仁们提供一个入门例子
1、新建一个web project,如下面的例子为Storm
2、在工程中目录如下图所示
Storm.
│ .classpath
│ .project
│ index.html
│ tree.txt
│
├─.settings
│ com.spket.js.prefs
│ org.eclipse.core.resources.prefs
│
├─src
│ └─com
│ │ TestDWR.java
│ │ TestWebService.java
│ │
│ └─service
│ HelloWorld.java
│
└─WEB-INF
│ server-config.wsdd
│ web.xml
│
├─attachments
├─classes
│ └─com
│ │ TestDWR.class
│ │ TestWebService.class
│ │
│ └─service
│ HelloWorld.class
│
└─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
3、编写java类
public class HelloWorld { private String name = "qingfeng"; public String getName(){ return name; } public String hiHello(String name) { return "Hi:" + name; } }
4、修改web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <servlet> <servlet-name>AxisServlet</servlet-name> <servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
5、修改 server-config.wsdd文件,与web.xml同一级目录,内容如下,在别面拷贝一个,修改自己类及方法
<?xml version="1.0" encoding="UTF-8"?> <deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> <globalConfiguration> <parameter name="sendMultiRefs" value="true"/> <parameter name="disablePrettyXML" value="true"/> <parameter name="dotNetSoapEncFix" value="true"/> <parameter name="enableNamespacePrefixOptimization" value="false"/> <parameter name="sendXMLDeclaration" value="true"/> <parameter name="sendXsiTypes" value="true"/> <parameter name="attachments.implementation" value="org.apache.axis.attachments.AttachmentsImpl"/> </globalConfiguration> <handler type="java:org.apache.axis.handlers.http.URLMapper" name="URLMapper"/> <service name="hello" provider="java:RPC"> <parameter name="className" value="com.service.HelloWorld" /> <parameter name="allowedMethods" value="*" /> </service> <service name="test" provider="java:RPC"> <parameter name="className" value="com.TestDWR" /> <parameter name="allowedMethods" value="*" /> </service> <transport name="http"> <requestFlow> <handler type="URLMapper"/> </requestFlow> </transport> </deployment>
6、将相关的jar加上,如上述目录所示,将工程布置到tomcat中,
7、访问发布的路径,如我本地为:http://localhost:8080/Storm/services
8、若在IE中,看到如下所示,就表明成功了
And now... Some Services
hello (wsdl)
test (wsdl)
9、下面我们再写一个测试类,提供外界访问,在工程目录中com.TestWebService类,代码如下所示
package com; import javax.xml.rpc.ServiceException; import org.apache.axis.client.Call; import org.apache.axis.client.Service; public class TestWebService { public static void test2() { String url = "http://localhost:8080/Storm/services/hello"; Service service = new Service(); Call call; String name = "东东和范范"; try { call = (Call) service.createCall(); call.setTargetEndpointAddress(new java.net.URL(url)); call.setOperationName("helloName"); String res = (String) call.invoke(new Object[] { name }); System.out.println(res); } catch (ServiceException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } /** * @param args */ public static void main(String[] args) { test2(); } }
10、在控制台输出【hello:东东和范范】表明成功。
11、附上整个项目源代码