webservice 远程数据交互技术
1.导入jar包(如果是 maven项目导入项目坐标)
2.创建服务
3.测试服务
我们使用maven来做测试服务
pom.xml文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.baidu.webservicetest01</groupId><artifactId>webservicetest01</artifactId><version>0.0.1-SNAPSHOT</version><!-- jdk版本1.7 --><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>2.3.2</version><configuration><source>1.7</source><target>1.7</target></configuration></plugin></plugins></build><dependencies><!-- cxf ?ws开发包 --><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>3.0.1</version></dependency><!-- jetty ?服务器包 --><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http-jetty</artifactId><version>3.0.1</version></dependency><!-- 使用log4j日志实现 --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.12</version></dependency><!-- 使用rs客户端 --><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-rs-client</artifactId><version>3.0.1</version></dependency></dependencies></project>
服务接口
package com.baidu.test;import javax.jws.WebMethod;import javax.jws.WebService;@WebServicepublic interface TestInterf {@WebMethodpublic void eat();}
服务实现类
package com.baidu.test;import javax.jws.WebService;@WebServicepublic class TestImp implements TestInterf {public void eat() {System.out.println("该吃饭了");}}
开启服务:
package com.baidu.test;import javax.xml.ws.Endpoint;public class PublishTest {public static void main(String[] args) {TestInterf tt=new TestImp();String address="http://localhost:9991/TestInterf";Endpoint.publish(address, tt);System.out.println("服务启动了");}}
新建项目 和服务器的包结构必须相同
测试项目的 测试接口和服务器的接口必须一致
package com.baidu.test1;import javax.jws.WebMethod;import javax.jws.WebService;@WebService ?//标识可以连接服务public interface TestInterf {@WebMethodpublic void eat();}
测试服务
package com.baidu.test1;import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;public class Test02 {public static void main(String[] args) {// 编写客户端 调用发布WebService服务JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();jaxWsProxyFactoryBean.setServiceClass(TestInterf.class);jaxWsProxyFactoryBean.setAddress("http://localhost:9991/TestInterf");// 创建调用远程服务代理对象TestInterf proxy = (TestInterf) jaxWsProxyFactoryBean.create();proxy.eat();}}
pom.xml 客户pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.baidu.webservicetest02</groupId><artifactId>webservicetest02</artifactId><version>0.0.1-SNAPSHOT</version><build> ???????<plugins> ???????????<plugin> ???????????????<groupId>org.apache.maven.plugins</groupId> ???????????????<artifactId>maven-compiler-plugin</artifactId> ???????????????<version>2.3.2</version> ???????????????<configuration> ???????????????????<source>1.7</source> ???????????????????<target>1.7</target> ???????????????</configuration> ???????????</plugin> ???????</plugins> ???</build><dependencies><!-- 要进行jaxws 服务开发 --> ?<dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>3.0.1</version></dependency><!-- 内置jetty web服务器 ?--><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http-jetty</artifactId><version>3.0.1</version></dependency><!-- 日志实现 --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.12</version></dependency></dependencies></project>
webService之helloword
原文地址:http://www.cnblogs.com/fjkgrbk/p/webService.html