webservice应用
什么是webservicewebservice是一种跨编程语言和跨操作系统平台的远程调用技术。 Web服务:基于HTTP和XML的技术,HTTP是互联网上应用最为广泛的一种网络协议,而XML是跨平台的基础。 Java 就有?Apache Axis1、Apache Axis2、Codehaus XFire、Apache CXF、Apache Wink、Jboss ?RESTEasyd等等 WebService三要素SOAP (Simple Object?Access Protocol):简易对象访问协议,soap用来描述传递信息的格式。 WSDL (WebServices Description Language):Web服务描述语言,用来描述如何访问具体的接口。 UDDI (Universal Description Discovery and?Integration):通用描述、发现及整合,用来管理、分发、查询webService。 WebService的应用场合跨越防火墙通信:应用程序可以使用HTTP和XML消息等标准在基于Web的应用程序之间交换信息,从而跨越防火墙 应用程序集成:何应用程序理论上都可以通过SOAP消息与任何其他应用程序进行通信 软件复用:软件复用是在软件开发中避免重复劳动的解决方案 WebService开发实战一、原生态客户端编写1.打开cmd,执行wsimport生成代码。 wsimport -s .http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl? 2.如图所示,代码生成在D:MyTestwebservice。会生成class和java文件。删除class文件 ? 3、开始编写查询手机号归属地客户端? eclipse创建工程,将生成的代码copy到你自己想要的包下,然后编写第一个WebService客户端接口 ? public class PhoneAddressClient { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(getPhoneAddress("15501529XXX")); } public static String getPhoneAddress(String phone) { // 1.实例化生成的服务类 MobileCodeWS ws = new MobileCodeWS(); // 2.调用服务类的方法获取接口实例 MobileCodeWSSoap soap = ws.getMobileCodeWSSoap(); // 3.通过接口直接获取数据 return soap.getMobileCodeInfo(phone,""); } } 15501529XXX:江苏 苏州 江苏联通GSM卡 二、自定义WebService服务端并发布服务编写客户端测试?1.编写服务接口 /** * 服务端 * @author zhoudoujun01 * */ public interface FisrtWebService { String getAddressByPhoneNo(String phoneNo); } 2.编写接口实现类 @WebService public class FisrtWebServiceImpl implements FisrtWebService { public String getAddressByPhoneNo(String phoneNo) { return phoneNo + ": 归属地是上海"; } } 3.发布服务 public class PublishServerTest { public static void main(String[] args) { //参数1:服务被访问的url ip+端口号+服务名 //参数2:实现类 Endpoint.publish("http://localhost:9999/getAddress",new FisrtWebServiceImpl()); System.out.println("服务发布成功"); } } 4.验证服务是否发布成功? http://localhost:9999/getAddress?wsdl 5.编写客户端(同上操作) ? 三、利用cxf开发webserviceApache CXF = Celtix + XFire,Apache CXF 的前身叫 Apache CeltiXfire,现在已经正式更名为 Apache CXF 了,以下简称为 CXF 支持多种标准
多种传输方式、Bindings、Data Bindings 和 Format
开发步骤: 1、新建工程maven web:cxf_spring,导入pom.xml <properties> <cxf.version>3.0.8</cxf.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <type>jar</type> <scope>test</scope> </dependency> <!-- spring的jar包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.1.6.RELEASE</version> </dependency> <!-- cxf的jar包 --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.geronimo.specs</groupId> <artifactId>geronimo-ws-metadata_2.0_spec</artifactId> <version>1.1.3</version> </dependency> </dependencies> <build> <finalName>cxf_spring</finalName> </build> 2、web.xml添加 <web-app> <display-name>Archetype Created Web Application</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:spring-cxf-config.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app> ? 3、新建applicationContext-cxf.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <!-- Cxf WebService 服务端示例 --> <jaxws:endpoint id="Helloservice" implementor="com.adou.service.impl.HelloServiceImpl" address="/hello"/> </beans> ? 4、新建IHelloService @WebService public interface IHelloService { @WebMethod String sayHello(@WebParam(name="username")String username); } public class HelloServiceImpl implements IHelloService { public String sayHello(String username) { return "hello " + username; } } ? 5、启动测试 http://localhost:8080/cxf_spring 6、客户端调用TestHellServiceImpl ? public class TestHellServiceImpl { @Test public void sayHello(){ IHelloService helloService = new HelloServiceImpl(); System.out.println(helloService.sayHello("tom")); } } 7、Demo地址:cxf-spring (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |