标签:
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);
}
} 
VB%W6ZJ)G]SP.jpg)
class HelloClient{
public static void main(String args[]) {
HelloService service = new HelloService();
Hello helloProxy = service.getHelloPort();
String hello = helloProxy.hello("你好");
System.out.println(hello);
}
}
package com.myeclipse.wsExample;
//Generated by MyEclipse
public interface IHelloWorldService {
public String example(String message);
}
package com.myeclipse.wsExample;
//Generated by MyEclipse
public class HelloWorldServiceImpl implements IHelloWorldService {
public String example(String message) {
return message;
}
}
<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>
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();
}
}
}
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[]{});
}
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-1.4.1-bin.zip
axis2-1.4.1-war.zip
http://ws.apache.org/axis2/
public class HelloWorld{
public String getName(String name)
{
return "你好 " + name;
}
public int add(int a,int b)
{
return a+b;
}
}
http://localhost:8080/axis2/services/listServices
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等数据类型
}
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"));
<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 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>
标签:
原文地址:http://www.cnblogs.com/guanshan/p/guan2015-6-10__1.html