JAX-WS Web服务Eclipse教程
时间:2020-02-23 14:35:18 来源:igfitidea点击:
在本教程中,我们将看到我们如何逐步开发JAX-WS端点和客户端。
准备工作
- JDK 1.6
- Eclipse IDE
创建JAX-WS WebService端点的步骤。
1)打开Eclipse IDE 2)创建名为"JAXWSERVER"的Java项目
3)创建名为"org.igi.javapostssforlearning.webservice"的新包
4)创建JAXWSService Endpoint接口。
HelloWorld.Java.
package org.igi.javapostsforlearning.webservice;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
@WebMethod public String helloWorld(String name);
}
5)创建JAXWSService Endpoint实现类。
helloWorldimpl.java.
package org.igi.javapostsforlearning.webservice;
import javax.jws.WebService;
@WebService(endpointInterface="org.igi.javapostsforlearning.webservice.HelloWorld")
public class HelloWorldImpl implements HelloWorld{
public String helloWorld(String name) {
return "Hello world from "+name;
}
}
6)创建端点发布者。
HelloWorldwspublisher.java.
package org.igi.javapostsforlearning.webservice;
import javax.xml.ws.Endpoint;
public class HelloWorldWSPublisher {
public static void main(String[] args) {
Endpoint.publish("http://localhost:8080/WS/HelloWorld",new HelloWorldImpl());
}
}
运行上面的程序。
WebService已发布。
我们可以在http://localhost:8080/ws/helloWorld查看服务WSDL?
创建JAXWS客户端的步骤
1)打开Eclipse并创建一个新的Java Project JaxWsClient。
3)现在我们需要生成客户端Stubs.so打开命令行,然后输入wsimport命令:
cd %project_home%/src wsimport -s . http://localhost:8080/WS/HelloWorld?wsdl
我们将在SRC-> org-> igi-> javapostsforlearning - > WebService下找到生成和编译的Java类
4)允许现在创建客户类。
在src-> org.igi.javapostssforlearning.webservice.client下创建Jaxwsclient.java.webservice.Client
package org.igi.javapostsforlearning.webservice.client;
import org.igi.javapostsforlearning.webservice.HelloWorld;
import org.igi.javapostsforlearning.webservice.HelloWorldImplService;
public class JAXWSClient {
/**
* @author igi Mandliya
*/
public static void main(String[] args) {
HelloWorldImplService helloWorldService = new HelloWorldImplService();
HelloWorld helloWorld = helloWorldService.getHelloWorldImplPort();
System.out.println(helloWorld.helloWorld("igi"));
}
}
5)运行上面的程序,我们将得到以下输出。
Hello world from igi

