1.什么是WebServices
具体的相关概念和专业解释大家可以去网上查找。我这里就说的通俗一点,就是别人可以通过你项目的网络地址,调用你的方法,具体使用方法请见下文。
2.创建一个webservices项目
我这里使用的工具是IDEA,大家如果使用的eclipse也不要紧,只需之后手动导入相关jar包,再修改一下wsdl文件即可。
1)创建新工程
File->New->Project...,如下图2-1:
图2-1
2)选择webServices工程
这里我们选择Use library而不选择Download,因为Download的话有可能会因为网络原因连接不到资源,导致一直下载不了jar包。Vservion选择apache Axis。具体步骤见图2-2:
图2-2
3)选择jar包
进入axis jar包所在目录,选择所有jar包,然后点击OK。详情见图2-3:
图2-3
4)完成工程创建
输入工程名字,点击Filish,详情见图2-4
图2-4
3.为工程导入jar包
File->Project Structure->problem->Fix->Add...
如果是eclipse,就用eclipse的方式把jar导入工程中,这里就不详细说明了。详情见图3-1
图3-1
4.编辑wsdd文件
如果是IDEA,你会发现工具已经在WEB-INF下生成好了server-config.wsdd,同时src下建好了example包,包下有HelloWord.java,就不用我们手动书写了,如果是eclipse,请在WEB-INFO下手动创建server-config.wsdd,src下建example包,包下建HelloWord.java,具体代码如下:
server-config.wsdd:
<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="adminPassword" value="admin"/>
<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"/>
<requestFlow>
<handler type="java:org.apache.axis.handlers.JWSHandler">
<parameter name="scope" value="session"/>
</handler>
<handler type="java:org.apache.axis.handlers.JWSHandler">
<parameter name="scope" value="request"/>
<parameter name="extension" value=".jwr"/>
</handler>
</requestFlow>
</globalConfiguration>
<handler name="LocalResponder" type="java:org.apache.axis.transport.local.LocalResponder"/>
<handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper"/>
<handler name="Authenticate" type="java:org.apache.axis.handlers.SimpleAuthenticationHandler"/>
<service name="AdminService" provider="java:MSG">
<parameter name="allowedMethods" value="AdminService"/>
<parameter name="enableRemoteAdmin" value="false"/>
<parameter name="className" value="org.apache.axis.utils.Admin"/>
<namespace>http://xml.apache.org/axis/wsdd/</namespace>
</service>
<service name="Version" provider="java:RPC">
<parameter name="allowedMethods" value="getVersion"/>
<parameter name="className" value="org.apache.axis.Version"/>
</service>
<service name="SOAPMonitorService" provider="java:RPC">
<parameter name="allowedMethods" value="publishMessage"/>
<parameter name="scope" value="Application"/>
<parameter name="className" value="org.apache.axis.monitor.SOAPMonitorService"/>
</service>
<service name="HelloWorld" provider="java:RPC" style="document" use="literal">
<parameter name="className" value="example.HelloWorld"/>
<parameter name="allowedMethods" value="*"/>
<parameter name="scope" value="Application"/>
<namespace>http://example</namespace>
</service>
<handler name="soapmonitor" type="java:org.apache.axis.handlers.SOAPMonitorHandler">
<parameter name="wsdlURL" value="/axis/SOAPMonitorService-impl.wsdl"/>
<parameter name="serviceName" value="SOAPMonitorService"/>
<parameter name="namespace" value="http://tempuri.org/wsdl/2001/12/SOAPMonitorService-impl.wsdl"/>
<parameter name="portName" value="Demo"/>
</handler>
<transport name="http">
<requestFlow>
<handler type="URLMapper"/>
<handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/>
<!--comment following line for REMOVING wsdl spying via SOAPMonitor-->
<handler type="soapmonitor"/>
</requestFlow>
<responseFlow>
<!--comment following line for REMOVING wsdl spying via SOAPMonitor-->
<handler type="soapmonitor"/>
</responseFlow>
<parameter name="qs:list" value="org.apache.axis.transport.http.QSListHandler"/>
<parameter name="qs:wsdl" value="org.apache.axis.transport.http.QSWSDLHandler"/>
<parameter name="qs.list" value="org.apache.axis.transport.http.QSListHandler"/>
<parameter name="qs.method" value="org.apache.axis.transport.http.QSMethodHandler"/>
<parameter name="qs:method" value="org.apache.axis.transport.http.QSMethodHandler"/>
<parameter name="qs.wsdl" value="org.apache.axis.transport.http.QSWSDLHandler"/>
</transport>
<transport name="local">
<responseFlow>
<handler type="LocalResponder"/>
</responseFlow>
</transport>
</deployment>
HelloWorld.java:
package example;/**
* Created by Ouyang on 2016/10/30. */public class HelloWorld { public String sayHelloWorldFrom(String from) { String result = "Hello, world, from " + from; System.out.println(result); return result; }}
5.配置tomcat
这里简单介绍一下idea下的配置过程,eclipse的就不多说了
1)打开Edit Configuration
见图5-1:
图5-1
2)添加tomcat
详情见图5-2
图5-2
3)配置tomcat
详情见图5-3
图5-3
4)配置项目地址
详情见图5-4
图5-4
6.启动服务
图6-1
输入http://localhost:8080/webservices/services,如果出现图6-2中的信息说明服务成功启动了,注:webservices为图5-4配置的项目地址
图6-2
7.客户端调用webservices方法
我们注意到server-config.wsdd中有这样一段代码:
<service name="HelloWorld" provider="java:RPC" style="document" use="literal">
<parameter name="className" value="example.HelloWorld"/>
<parameter name="allowedMethods" value="*"/>
<parameter name="scope" value="Application"/>
<namespace>http://example</namespace>
</service>
其中有style="document"和没有style="document"的参数传输方式是不同的,至于RPC-style和Document-style的具体区别,这里就不多介绍了,我这里就只单单介绍参数传输方式。
1)Document-style
保留style="document",随便新建一个项目,创建Test.java,代码如下:import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Vector;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.message.SOAPBodyElement;
import org.apache.axis.utils.XMLUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Element;
/**
* @author Ouyang
*
* 测试调用WebService
*/
public class TestHelloService {
private static final Log log = LogFactory.getLog(TestHelloService.class);
private static final String HELLO_SERVICE_ENDPOINT = "http://localhost:8080/webservices/services/HelloWorld?wsdl";//HelloWord的wsdl地址
public static void main(String[] args) {
TestHelloService tester = new TestHelloService();
tester.callSayHello();
}
public void callSayHello() {
try {
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(
HELLO_SERVICE_ENDPOINT));
SOAPBodyElement[] input = new SOAPBodyElement[1];
input[0] = new SOAPBodyElement(XMLUtils.StringToElement("http://HelloWorld", "sayHelloWorldFrom", "Ouyang"));//http://HelloWorld可以随便填,sayHelloWorldFrom是方法名,Ouyang是参数值
try {
//远程调用发布的方法
Vector rtn = (Vector) call.invoke(input);
Element result=((SOAPBodyElement)rtn.get(0));
System.out.println(result.getFirstChild().getNodeValue());
return;
} catch (IOException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ServiceException e) {
e.printStackTrace();
}
log.error("call sayHello service error!");
}
}
http://localhost:8080/webservices/services/HelloWorld?wsdl可由图6-2中,点击HelloWorld后的wsdl获得地址
运行TestHelloService,服务端和客户端的控住台都会打印出Hello, world, from Ouyang,说明运行成功。
2)RPC-style
document-style是不能传递两个以上的参数的,要传递两个以上的参数就只能用RPC-style(也有可能有大神能用document-style传递多个参数,只是我暂时不知道方法)。1>改写server-config.wsdd
停止axisDemo的tomcat,找如下代码:
<service name="HelloWorld" provider="java:RPC" style="document" use="literal">删掉style="document"
<parameter name="className" value="example.HelloWorld"/>
<parameter name="allowedMethods" value="*"/>
<parameter name="scope" value="Application"/>
<namespace>http://example</namespace>
</service>
2>改写HelloWrold
代码如下:
package example;/**重启tomcat
* Created by Ouyang on 2016/10/30.
*/
public class HelloWorld {
public String sayHelloWorldFrom(String from,String from2) {
String result = "Hello, world, from " + from + "," + from2;
System.out.println(result);
return result;
}
}
3>改写TestHelloService
代码如下:
import java.io.IOException;运行TestHelloService,服务端和客户端的控住台都会打印出Hello, world, from Ouyang,CSDN说明运行成功!简单的一个webservcies入门案例就到此为止了,再深入的研究就要靠各位*发挥了。
import java.net.MalformedURLException;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* @author Ouyang
*
* 测试调用WebService
*/
public class TestHelloService {
private static final Log log = LogFactory.getLog(TestHelloService.class);
private static final String HELLO_SERVICE_ENDPOINT = "http://localhost:8080/webservices/services/HelloWorld?wsdl";//HelloWord的wsdl地址
public static void main(String[] args) {
TestHelloService tester = new TestHelloService();
tester.callSayHello();
}
public void callSayHello() {
try {
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(
HELLO_SERVICE_ENDPOINT));
call.setOperationName(new QName("http://",
"sayHelloWorldFrom"));//"http://"可以随便填,sayHelloWorldFrom就是方法名
call.addParameter("from", org.apache.axis.Constants.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
call.addParameter("from2", org.apache.axis.Constants.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);//form和from2也可以改为其他英文字母
call.setReturnType(org.apache.axis.Constants.XSD_STRING);
try {
//远程调用发布的方法
String ret = (String) call.invoke(new Object[] { "Ouayng","CSDN" });//"Ouayng","CSDN"分别对应sayHelloWorldFrom中的from和from2
System.out.println("The return value is:" + ret);
return;
} catch (IOException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ServiceException e) {
e.printStackTrace();
}
log.error("call sayHello service error!");
}
}
8.相关文章推荐
以下两篇文章是我入门学习时的参考,大家可以参考一下:
http://nopainnogain.iteye.com/blog/791525
http://www.iteye.com/problems/46115