基于CXF的WebService实例
2014-08-06 星期三 Nesoft 终于有时间可以发一博文了,这段时间不是很忙,可以学点东西,为什么要写呢,因为在前段时间的项目开发中,用到很多网友写的代码和一些新技术点,所以感觉自己也要有分享精神,研究出来的东西就和大家一块分享,不是每个人都要走弯路才能成长的。 WebService的基础知道在这里不说了,WebService的出现使异构平台的互操作成为可能,使用功能模块化,分布化 WebService其实可以理解为一组通信协议集,只要按照这种协议来访问,就可以访问其对应服务,WebService的协议其实就是SOAP协议,发布WebService和编写WebService客户端,只要遵循这种协议就可以,其实现方式也很多: 可以自己构建WSDL描述文件,然后接收Http的请求,然后解析SOAP的消息 做相应调用。XML是SOAP的载体,其实WebService就是对XML的schema和xml元素的操作,目前已经有很多的开源的库可以供我们使用,所以不用再自己去实现最低层的东西。如Axis、CXF、JAX-WS 闲话不多说,直接看实例,说理论对程序员们来说没太大作用,给个Demo,一切都明了了: 以下是个基于CXF库的WebService: 1. 导入需求的库文件。 2. WebService的相关类,直接上代码 服务接口: <span style="font-family:SimSun;font-size:14px;">package com.test; import javax.jws.WebResult; import javax.jws.WebService; @WebService public interface HelloWord { public @WebResult String sayHello(String name); // public @WebResult(partName = "userName") String sayUserName(@WebParam(name="user") User user) ; // // public @WebResult(partName = "o") ListObject findUsers(); } </span> 实现类: <span style="font-family:SimSun;font-size:14px;">package com.test; import java.util.ArrayList; import javax.jws.WebService; @WebService public class HelloWordImpl implements HelloWord { public String sayHello(String name) { System.out.println("say hello"); return "hello12 "+" "+name+" ok!"; } public String sayUserName(User user) { String name = user.getName() ; return "hello " + name ; } public ListObject findUsers() { ArrayList<Object> list = new ArrayList<Object>(); list.add(new User(1,"AA")) ; list.add(new User(2,"BB")) ; list.add(new User(3,"CC")) ; ListObject o = new ListObject(); o.setList(list); return o; } } </span> 3.bean.xml文件的配置: <span style="font-family:SimSun;font-size:14px;"><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"> <import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <jaxws:endpoint id="helloWorld" implementor="com.test.HelloWordImpl" address="/HelloWorld" /> </beans></span> <span style="font-family:SimSun;font-size:14px;"><?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/beans.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class> org.apache.cxf.transport.servlet.CXFServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/service/*</url-pattern> </servlet-mapping> </web-app> </span> 有Web基础的人不难看出,其实就是在Web.xml中加入启动时监听器,启动时会去加载contextConfigLocation,就会加载bean.xml的内容,然后CXFServlet就初始化完成了,WebService也就对外发布完成了。 5.下面请看wsdl的内容:(http://localhost:8082/WebServiceA/service/HelloWorld?wsdl) <span style="font-family:SimSun;font-size:14px;"> <?xml version="1.0" encoding="UTF-8" ?> - <wsdl:definitions name="HelloWordImplService" targetNamespace="http://test.com/" xmlns:ns1="http://schemas.xmlsoap.org/wsdl/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://test.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> - <wsdl:types> - <xs:schema attributeFormDefault="unqualified" elementFormDefault="unqualified" targetNamespace="http://test.com/" xmlns:tns="http://test.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="sayHello" type="tns:sayHello" /> <xs:element name="sayHelloResponse" type="tns:sayHelloResponse" /> - <xs:complexType name="sayHello"> - <xs:sequence> <xs:element minOccurs="0" name="arg0" type="xs:string" /> </xs:sequence> </xs:complexType> - <xs:complexType name="sayHelloResponse"> - <xs:sequence> <xs:element minOccurs="0" name="return" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:schema> </wsdl:types> - <wsdl:message name="sayHelloResponse"> <wsdl:part element="tns:sayHelloResponse" name="parameters" /> </wsdl:message> - <wsdl:message name="sayHello"> <wsdl:part element="tns:sayHello" name="parameters" /> </wsdl:message> - <wsdl:portType name="HelloWord"> - <wsdl:operation name="sayHello"> <wsdl:input message="tns:sayHello" name="sayHello" /> <wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse" /> </wsdl:operation> </wsdl:portType> - <wsdl:binding name="HelloWordImplServiceSoapBinding" type="tns:HelloWord"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> - <wsdl:operation name="sayHello"> <soap:operation soapAction="" style="document" /> - <wsdl:input name="sayHello"> <soap:body use="literal" /> </wsdl:input> - <wsdl:output name="sayHelloResponse"> <soap:body use="literal" /> </wsdl:output> </wsdl:operation> </wsdl:binding> - <wsdl:service name="HelloWordImplService"> - <wsdl:port binding="tns:HelloWordImplServiceSoapBinding" name="HelloWordImplPort"> <soap:address location="http://localhost:8082/WebServiceA/service/HelloWorld" /> </wsdl:port> </wsdl:service> </wsdl:definitions></span> 6:OK,现在可以上客户端的代码: <span style="font-family:SimSun;font-size:14px;">package com.test; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; public class HelloWordClient { public static void main(String[] args) { JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.setAddress("http://localhost:8082/WebServiceA/service/HelloWorld?wsdl"); factory.setServiceClass(HelloWord.class); HelloWord helloWord = (HelloWord)factory.create(); System.out.println(helloWord.sayHello("aa")); System.out.println("~~~~~~~~~~~~~~~~"); } }</span> 好,现在大功成了,可以访问了 Demo下载地址为:http://download.csdn.net/detail/tqtihihc/7722125 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |