webservice


在eclipse中新建一个webservice工程,目的是引入Xfile的jar包,增加service.xml配置,以及在web.xml中新增一个servlet。
修改service.xml文件,增加服务节点:
     <service>
         <name>HelloService</name>
         <serviceClass>com.Hello</serviceClass>
         <implementationClass>com.HelloService</implementationClass>
     </service>
布署完成后,可以通过地址:
     http://localhost:8080/webservice/services/HelloService?wsdl 查看web服务中可调用的接口。
public interface Hello {
    public int add(int i,int j);
    public void print(String s);
}


public class HelloService implements Hello {
    public int add(int i,int j) {
    return i+j;
    }

public void print(String s) {
// TODO Auto-generated method stub
System.out.println("--->"+s);
}
    
}
Xfile调用:首先在eclipse中导入xfile core 1.2.jar和xfile http client.jar
调用代码如下:
Service serModel = new ObjectServiceFactory().create(Hello.class);
XFireProxyFactory factory = new XFireProxyFactory(XFireFactory.newInstance().getXFire());
String helloURL = "http://localhost:8080/webservice/services/HelloService";
try {
Hello h = (Hello) factory.create(serModel,helloURL);
h.print(" liping");
} catch (MalformedURLException e) {
e.printStackTrace();
} 
axis调用:首先在eclipse中导入axis包,调用代码如下:

String helloURL = "http://localhost:8080/webservice/services/HelloService";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(helloURL));
call.setOperationName("print");
call.invoke(new Object[] {" liping"});



你可能感兴趣的:(webservice)