
正文
webService服务简单实现
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
首先写一个简单的webservice服务
package com.service.impl; import java.util.Date; import javax.jws.WebService; import com.service.HelloWorld; @WebService
public class HelloWorlds implements HelloWorld{ @Override
public String sayHi(String name) {
// TODO Auto-generated method stub
return name+"您好!现在时间是:"+new Date();
}
}
接下来就是将weibservice服务发布暴露出来
package com.action; import javax.xml.ws.Endpoint; import com.service.HelloWorld;
import com.service.impl.HelloWorlds; public class ServiceMain { public static void main(String[] args) {
// TODO Auto-generated method stub
HelloWorld hw = new HelloWorlds();
//调用Endpoint的publish方法发布Web Service
Endpoint.publish("http://127.0.0.1/helloWorld", hw);
System.out.println("Web Service暴露成功!");
} }
通过访问http://127.0.0.1/helloWorld?wsdl就可以看到服务的描述,也就是webservice三大组件中的wsdl.
接下来就是创建webservice服务的客户端用来调用服务,记住在创建的时候在WSDL URL输入上面查看描述服务的地址就可以自动生成对应的webservice客户端
我的生成的文件如下
最后就是写测试程序开始调用webservice发布的服务了
package com.action; import com.service.impl.HelloWorlds;
import com.service.impl.HelloWorldsService;
public class WebService { public static void main(String[] args) {
// TODO Auto-generated method stub
HelloWorldsService service=new HelloWorldsService();
HelloWorlds action=service.getHelloWorldsPort();
String message=action.sayHi("JIM");
System.out.println("调webservice:"+message);
} }







