一、利用jdk web服務api實現,這裏使用基於 SOAP message 的 Web 服務
1.首先建立一個Web services EndPoint:
Java代碼
package Hello;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.xml.ws.Endpoint;
@WebService
public class Hello {
@WebMethod
public String hello(String name) {
return "Hello, " + name + "/n";
}
public static void main(String[] args) {
// create and publish an endpoint
Hello hello = new Hello();
Endpoint endpoint = Endpoint.publish("http://localhost:8080/hello", hello);
}
}
package Hello;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.xml.ws.Endpoint;
@WebService
public class Hello {
@WebMethod
public String hello(String name) {
return "Hello, " + name + "/n";
}
public static void main(String[] args) {
// create and publish an endpoint
Hello hello = new Hello();
Endpoint endpoint = Endpoint.publish("http://localhost:8080/hello", hello);
}
}
2.使用 apt 編譯 Hello.java(例:apt -d [存放編譯後的文件目錄] Hello.java ) ,會生成 jaws目錄
3.使用java Hello.Hello運行,然後將瀏覽器指向http://localhost:8080/hello?wsdl就會出現下列顯示
4.使用wsimport 生成客戶端
使用如下:wsimport -p . -keep http://localhost:8080/hello?wsdl
5.客戶端程序:
Java代碼
class HelloClient{
public static void main(String args[]) {
HelloService service = new HelloService();
Hello helloProxy = service.getHelloPort();
String hello = helloProxy.hello("你好");
System.out.println(hello);
}
}
class HelloClient{
public static void main(String args[]) {
HelloService service = new HelloService();
Hello helloProxy = service.getHelloPort();
String hello = helloProxy.hello("你好");
System.out.println(hello);
}
}
二、使用xfire,我這裏使用的是myeclipse集成的xfire進行測試的
利用xfire開發WebService,可以有三種方法:
1一種是從javabean 中生成;
2 一種是從wsdl文件中生成;
3 還有一種是自己建立webservice
步驟如下:
用myeclipse建立webservice工程,目錄結構如下:
首先建立webservice接口,
代碼如下:
Java代碼
package com.myeclipse.wsExample;
//Generated by MyEclipse
public interface IHelloWorldService {
public String example(String message);
}
package com.myeclipse.wsExample;
//Generated by MyEclipse
public interface IHelloWorldService {
public String example(String message);
}
Java代碼
package com.myeclipse.wsExample;
//Generated by MyEclipse
public class HelloWorldServiceImpl implements IHelloWorldService {
public String example(String message) {
return message;
}
}
package com.myeclipse.wsExample;
//Generated by MyEclipse
public class HelloWorldServiceImpl implements IHelloWorldService {
public String example(String message) {
return message;
}
}
修改service.xml 文件,加入以下代碼:
Xml代碼
<service>
<name>HelloWorldService</name>
<serviceClass>
com.myeclipse.wsExample.IHelloWorldService
</serviceClass>
<implementationClass>
com.myeclipse.wsExample.HelloWorldServiceImpl
</implementationClass>
<style>wrapped</style>
<use>literal</use>
<scope>application</scope>
</service>
<service>
<name>HelloWorldService</name>
<serviceClass>
com.myeclipse.wsExample.IHelloWorldService
</serviceClass>
<implementationClass>
com.myeclipse.wsExample.HelloWorldServiceImpl
</implementationClass>
<style>wrapped</style>
<use>literal</use>
<scope>application</scope>
</service>
把整個項目部署到tomcat服務器中 ,打開瀏覽器,輸入http://localhost:8989/HelloWorld/services/HelloWorldService?wsdl
客戶端實現如下:
Java代碼
package com.myeclipse.wsExample.client;
import java.net.MalformedURLException;
import java.net.URL;
import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.client.Client;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import com.myeclipse.wsExample.IHelloWorldService;
public class HelloWorldClient {
public static void main(String[] args) throws MalformedURLException, Exception {
// TODO Auto-generated method stub
Service s=new ObjectServiceFactory().create(IHelloWorldService.class);
XFireProxyFactory xf=new XFireProxyFactory(XFireFactory.newInstance().getXFire());
String url="http://localhost:8989/HelloWorld/services/HelloWorldService";
try
{
IHelloWorldService hs=(IHelloWorldService) xf.create(s,url);
String st=hs.example("zhangjin");
System.out.print(st);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
package com.myeclipse.wsExample.client;
import java.net.MalformedURLException;
import java.net.URL;
import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.client.Client;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import com.myeclipse.wsExample.IHelloWorldService;
public class HelloWorldClient {
public static void main(String[] args) throws MalformedURLException, Exception {
// TODO Auto-generated method stub
Service s=new ObjectServiceFactory().create(IHelloWorldService.class);
XFireProxyFactory xf=new XFireProxyFactory(XFireFactory.newInstance().getXFire());
String url="http://localhost:8989/HelloWorld/services/HelloWorldService";
try
{
IHelloWorldService hs=(IHelloWorldService) xf.create(s,url);
String st=hs.example("zhangjin");
System.out.print(st);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
這裏再說點題外話,有時候我們知道一個wsdl地址,比如想用java客戶端引用.net 做得webservice,使用myeclipse引用,但是卻出現無法通過驗證的錯誤,這時我們可以直接在類中引用,步驟如下:
Java代碼
public static void main(String[] args) throws MalformedURLException, Exception {
// TODO Auto-generated method stub
Service s=new ObjectServiceFactory().create(IHelloWorldService.class);
XFireProxyFactory xf=new XFireProxyFactory(XFireFactory.newInstance().getXFire());
//遠程調用.net開發的webservice
Client c=new Client(new URL("http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl"));
Object[] o=c.invoke("qqCheckOnline", new String[]{"531086641","591284436"});
//調用.net本機開發的webservice
Client c1=new Client(new URL("http://localhost/zj/Service.asmx?wsdl"));
Object[] o1=c1.invoke("HelloWorld",new String[]{});
}
public static void main(String[] args) throws MalformedURLException, Exception {
// TODO Auto-generated method stub
Service s=new ObjectServiceFactory().create(IHelloWorldService.class);
XFireProxyFactory xf=new XFireProxyFactory(XFireFactory.newInstance().getXFire());
//遠程調用.net開發的webservice
Client c=new Client(new URL("http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl"));
Object[] o=c.invoke("qqCheckOnline", new String[]{"531086641","591284436"});
//調用.net本機開發的webservice
Client c1=new Client(new URL("http://localhost/zj/Service.asmx?wsdl"));
Object[] o1=c1.invoke("HelloWorld",new String[]{});
}
三、使用axis1.4調用webservice方法
前提條件:下載axis1.4包和tomcat服務器 ,並將axis文件夾複製到tomcat服務器的webapp文件夾中
這裏我就說一下最簡單的方法:
首先建立一個任意的java類(例如:HelloWorld.java),複製到axis文件夾下,將其擴展名改爲jws,然後重新啓動tomcat,在瀏覽器中輸入http://localhost:8989/axis/HelloWorld.jws?wsdl,就會得到一個wsdl文件,其客戶端調用方法如下:
Java代碼
import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.ServiceFactory;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import javax.xml.namespace.QName;
public class TestHelloWorld {
public static void main(String[] args) throws MalformedURLException, ServiceException, RemoteException {
// TODO Auto-generated method stub
String wsdlUrl ="http://localhost:8989/axis/HelloWorld.jws?wsdl";
String nameSpaceUri ="http://localhost:8989/axis/HelloWorld.jws";
String serviceName = "HelloWorldService";
String portName = "HelloWorld";
ServiceFactory serviceFactory = ServiceFactory.newInstance();
Service afService =serviceFactory.createService(new URL(wsdlUrl),new QName(nameSpaceUri, serviceName));
HelloWorldInterface proxy = (HelloWorldInterface)afService.getPort(new QName(nameSpaceUri, portName),HelloWorldInterface.class);
System.out.println("return value is "+proxy.getName("john") ) ;
}
}
import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.ServiceFactory;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import javax.xml.namespace.QName;
public class TestHelloWorld {
public static void main(String[] args) throws MalformedURLException, ServiceException, RemoteException {
// TODO Auto-generated method stub
String wsdlUrl ="http://localhost:8989/axis/HelloWorld.jws?wsdl";
String nameSpaceUri ="http://localhost:8989/axis/HelloWorld.jws";
String serviceName = "HelloWorldService";
String portName = "HelloWorld";
ServiceFactory serviceFactory = ServiceFactory.newInstance();
Service afService =serviceFactory.createService(new URL(wsdlUrl),new QName(nameSpaceUri, serviceName));
HelloWorldInterface proxy = (HelloWorldInterface)afService.getPort(new QName(nameSpaceUri, portName),HelloWorldInterface.class);
System.out.println("return value is "+proxy.getName("john") ) ;
}
}
四、使用axis2開發webservice(這裏首先感謝李寧老師)
使用axis2 需要先下載
axis2-1.4.1-bin.zip
axis2-1.4.1-war.zip
同理,也需要將axis2複製到webapp目錄中
在axis2中部署webservice有兩種方法,
第一種是pojo方式,這種方式比較簡單,但是有一些限制,例如部署的類不能加上包名
第二種方式是利用xml發佈webservice,這種方法比較靈活,不需要限制類的聲明
下面分別說明使用方法:
1.pojo方式:在Axis2中不需要進行任何的配置,就可以直接將一個簡單的POJO發佈成WebService。其中POJO中所有的public方法將被髮布成WebService方法。先實現一個pojo類:
Java代碼
public class HelloWorld{
public String getName(String name)
{
return "你好 " + name;
}
public int add(int a,int b)
{
return a+b;
}
}
public class HelloWorld{
public String getName(String name)
{
return "你好 " + name;
}
public int add(int a,int b)
{
return a+b;
}
}
由於這兩個方法都是public類型,所以都會發布成webservice。編譯HelloWorld類後,將HelloWorld.class文件放到%tomcat%/webapps/axis2/WEB-INF/pojo目錄中(如果沒有pojo目錄,則建立該目錄),然後打開瀏覽器進行測試:
輸入一下url:
http://localhost:8080/axis2/services/listServices
會列出所有webservice
這是其中的兩個webservice列表,接着,在客戶端進行測試:
首先可以寫一個封裝類,減少編碼,代碼如下:
Java代碼
package MZ.GetWebService;
import javax.xml.namespace.QName;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
public class GetWSByAxis2 {
private static String EndPointUrl;
private static String QUrl="http://ws.apache.org/axis2";
private QName opAddEntry;
public String WSUrl;
public RPCServiceClient setOption() throws AxisFault
{
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
EndpointReference targetEPR = new EndpointReference(WSUrl);
options.setTo(targetEPR);
return serviceClient;
}
public QName getQname(String Option){
return new QName (QUrl,Option);
}
//返回String
public String getStr(String Option) throws AxisFault
{
RPCServiceClient serviceClient =this.setOption();
opAddEntry =this.getQname(Option);
String str = (String) serviceClient.invokeBlocking(opAddEntry,
new Object[]{}, new Class[]{String.class })[0];
return str;
}
// 返回一維String數組
public String[] getArray(String Option) throws AxisFault
{
RPCServiceClient serviceClient =this.setOption();
opAddEntry =this.getQname(Option);
String[] strArray = (String[]) serviceClient.invokeBlocking(opAddEntry,
new Object[]{}, new Class[]{String[].class })[0];
return strArray;
}
//從WebService中返回一個對象的實例
public Object getObject(String Option,Object o) throws AxisFault
{
RPCServiceClient serviceClient =this.setOption();
QName qname=this.getQname(Option);
Object object = serviceClient.invokeBlocking(qname, new Object[]{},new Class[]{o.getClass()})[0];
return object;
}
///////////////////////////////////////// 讀者可以自己封裝數據類型,如int,byte,float等數據類型
}
package MZ.GetWebService;
import javax.xml.namespace.QName;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
public class GetWSByAxis2 {
private static String EndPointUrl;
private static String QUrl="http://ws.apache.org/axis2";
private QName opAddEntry;
public String WSUrl;
public RPCServiceClient setOption() throws AxisFault
{
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
EndpointReference targetEPR = new EndpointReference(WSUrl);
options.setTo(targetEPR);
return serviceClient;
}
public QName getQname(String Option){
return new QName (QUrl,Option);
}
//返回String
public String getStr(String Option) throws AxisFault
{
RPCServiceClient serviceClient =this.setOption();
opAddEntry =this.getQname(Option);
String str = (String) serviceClient.invokeBlocking(opAddEntry,
new Object[]{}, new Class[]{String.class })[0];
return str;
}
// 返回一維String數組
public String[] getArray(String Option) throws AxisFault
{
RPCServiceClient serviceClient =this.setOption();
opAddEntry =this.getQname(Option);
String[] strArray = (String[]) serviceClient.invokeBlocking(opAddEntry,
new Object[]{}, new Class[]{String[].class })[0];
return strArray;
}
//從WebService中返回一個對象的實例
public Object getObject(String Option,Object o) throws AxisFault
{
RPCServiceClient serviceClient =this.setOption();
QName qname=this.getQname(Option);
Object object = serviceClient.invokeBlocking(qname, new Object[]{},new Class[]{o.getClass()})[0];
return object;
}
///////////////////////////////////////// 讀者可以自己封裝數據類型,如int,byte,float等數據類型
}
客戶端調用方法:
Java代碼
MZ.GetWebService.GetWSByAxis2 ws=new MZ.GetWebService.GetWSByAxis2();
ws.WSUrl="http://localhost:8989/axis2/services/HelloWorld";
HelloWorld hello= (HelloWorld)ws.getObject("getName", HelloWorld.class);
System.out.println(hello.getName("zhangjin"));
MZ.GetWebService.GetWSByAxis2 ws=new MZ.GetWebService.GetWSByAxis2();
ws.WSUrl="http://localhost:8989/axis2/services/HelloWorld";
HelloWorld hello= (HelloWorld)ws.getObject("getName", HelloWorld.class);
System.out.println(hello.getName("zhangjin"));
2.使用service.xml發佈webservice,這種方式和直接放在pojo目錄中的POJO類不同。要想將MyService類發佈成Web Service,需要一個services.xml文件,這個文件需要放在META-INF目錄中,該文件的內容如下:
Xml代碼
<service name="HelloWorld">
<description>
HelloWorld webservice
</description>
<parameter name="ServiceClass">
service.HelloWorld
</parameter>
<messageReceivers>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
</messageReceivers>
</service>
<service name="HelloWorld">
<description>
HelloWorld webservice
</description>
<parameter name="ServiceClass">
service.HelloWorld
</parameter>
<messageReceivers>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
</messageReceivers>
</service>
其中<service>元素用於發佈Web Service,一個<service>元素只能發佈一個WebService類,name屬性表示WebService名,如下面的URL可以獲得這個WebService的WSDL內容:
http://localhost:8080/axis2/services/myService?wsdl
除此之外,還有直接可以在其中制定webservice操作方法:可以這樣些service.xml文件
Java代碼
<service name="HelloWorld">
<description>
HelloWorld service
</description>
<parameter name="ServiceClass">
service.HelloWorld
</parameter>
<operation name="getName">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</operation>
<operation name="add">
<messageReceiver
class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</operation>
</service>
<service name="HelloWorld">
<description>
HelloWorld service
</description>
<parameter name="ServiceClass">
service.HelloWorld
</parameter>
<operation name="getName">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</operation>
<operation name="add">
<messageReceiver
class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</operation>
</service>