Java+Axis WebService接口开发实例
一、下载Axis的jar包
下载地址:http://ws.Apache.org/axis/
二、创建两个Web项目
1、WebService项目:负责提供接口,导入Axis的Jar包
(1)创建server-config.wsdd
<?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="TestWebService.jws" provider="java:RPC">
<!-- 调用的包名+类名 -->
<parameter name="className" value="com.donghaiair.test.TestWebsService" />
<parameter name="scope" value="request" />
<!-- 这*代表所有的方法 -->
<parameter name="allowedMethods" value="*" />
<messageReceiver
class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver" />
</service>
<transport name="http">
<requestFlow>
<handler type="URLMapper" />
</requestFlow>
</transport>
</deployment>
(2)在web.xml中添加
<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>
(3)创建接口类
package com.donghaiair.test;
public class TestWebsService {
public String testWebsServiceNull(){
return "test";
}
public String testWebsServiceOne(String s){
return "test:" + s;
}
public String testWebsServiceTwo(int i,String s){
return "test:" + i + "\n" + s;
}
}
2、WebServiceClient项目:负责调用接口,导入Axis的Jar包
package com.donghaiair.test;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
public class TestWebsService {
public static void main(String[] args) {
String url = "http://localhost:8080/WebService/services/TestWebService.jws";//
Service service = new Service();
Call call;
try {
call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(url) );
// call.setOperationName("testWebsServiceNull" );// 这是要调用的方法
// String res = (String) call.invoke(new Object[] {null});
call.setOperationName("testWebsServiceOne" );// 这是要调用的方法
String res = (String) call.invoke( new Object[] {"徐士宽"} );
// call.setOperationName("testWebsServiceTwo" );// 这是要调用的方法
// String res = (String) call.invoke( new Object[] {17,"徐士宽"} );
System.out.println(res);
}catch (MalformedURLException e) {
} catch (RemoteException e) {
e.printStackTrace();
} catch (ServiceException e) {
e.printStackTrace();
}
}
}
注释一:Jar包相关截图
注释二:项目先关截图
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。