server端:
服务:
@WebService(targetNamespace="")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public class TestServer {
public TestServer(){
System.out.println("register");
}
@WebMethod(action="toSayHello",operationName="toSayHello",exclude=false)
@WebResult(name="returnWord")//自定义该方法返回值在WSDL中相关的描述
public String sayHello(@WebParam(name="userName")String userName) {
System.out.println( "Hello:" + userName);
return "Hello:" + userName;
}
}
启动服务:
public class StartServer {
public static void main(String[] args) {
Endpoint.publish("http://localhost/webservice/", new TestServer());
}
}
可以访问http://localhost/webservice/?wsdl
client端
通过反编译
命令:
wsimport -s 需要生成JAVA类所在的文件夹路径 -keep http://localhost/webservice/?wsdl
拿到
TestServer.java和TestServerService.java
!!!!!注意:运行的时候服务要开着!!!!!
public class TestClient {
public static void main(String[] args) {
TestServerService test = new TestServerService();
System.out.println( test.getTestServerPort().toSayHello("sunhao") );//调用WEBSERVICE的sayHello方法
}
}
本文介绍了一个使用Java实现的WebService服务端与客户端的示例。服务端通过注解定义了RPC风格的SOAP服务,并实现了sayHello方法。客户端通过wsimport命令生成必要的Java类,进而调用了服务端的sayHello方法。
468

被折叠的 条评论
为什么被折叠?



