WebServices调用基本采用以下技术:
axis1 axis2 xfire cxf
目前应该cxf技术最新,前面的逐渐被淘汰。我们这里使用CXF来访问WebServices服务。
我们采用JDK1.6提供给我们的ws创建服务端
项目采用maven构建,pom.xml依赖如下
<dependency> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.4.3</version> </dependency> <dependency> <groupId>org.apache.cxf.karaf</groupId> <artifactId>apache-cxf</artifactId> <version>2.7.11</version> <type>pom</type> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging-api</artifactId> <version>1.1</version> </dependency> <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.1</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.9</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency>
1.创建接口,提供用注解提示编译器 这个是WebServices接口。
import javax.jws.WebService; @WebService public interface HelloWorld { public String sayHello(String str); }
2.完成接口的实现
import javax.jws.WebService; @WebService(endpointInterface = "com.yonyou.wsserver.HelloWorld",serviceName = "hello") public class WSClient implements HelloWorld { @Override public String sayHello(String str) { return "hello world"; } }
3.创建main函数启动服务端代码
import javax.xml.ws.Endpoint; public class ServerMain { public static void main(String[] args) { HelloWorld helloWorld = new WSClient(); Endpoint.publish("http://127.0.0.1:8080/hello",helloWorld); System.out.println("success"); } }
启动成功后访问一下 http://127.0.0.1:8080/hello?wsdl
接下来我们创建客户端调用服务端WebServices程序。我们使用wsdl2java,该工具在cxf的bin下
1.使用该工具 执行wsdl2java http://127.0.0.1:8080/hello?wsdl 会生成很多文件,如果有错误,直接注释或者删除就行了
2.编写代码,返回远程WebService的代理对象。执行对应接口方法就可以了
import com.yonyou.ws.server.impl.ImplMyFirstWS; public class WSClient { public static void main(String[] args) { ImplMyFirstWS client = new ImplMyFirstWS(); String str = client.insertData("高明"); System.out.println(str); } }正常返回结果 ok 我们搞定了
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。