知识大全 使用cxf写web service的简单实例

Posted

篇首语:枕上从妨一夜睡,灯前读尽十年诗。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 使用cxf写web service的简单实例相关的知识,希望对你有一定的参考价值。

实例步骤         第一步 在myeclipse中新建一个web项目名为webservicetest 并导入依赖的jar包(cxf spring apache mons相关)        mons logging jar        cxf jar        geronimo jaxws_ _spec jar        geronimo ws metadata_ _spec jar        neethi jar        cxf结合spring时所需jar包 此例子也需要这些 用到了spring上下文加载         spring asm RELEASE jar        spring beans RELEASE jar        spring context RELEASE jar        spring core RELEASE jar        spring expression RELEASE jar        spring aop RELEASE jar        spring web RELEASE jar        xmlschema core jar        jaxb api jar        wsdl j jar        jsr _api jar        mon annotations jar        jaxb impl jar        stax api jar        wstx asl jar        jetty util v jar        jetty continuation v jar        jetty         jetty io v jar        jetty security v jar        jetty server v jar        @WebService和@WebMethod是WSDL映射Annotation 这些Annotation将描述Web Service的WSDL文档元素和JAVA源代码联系在一起         @WebService annotation的元素name serviceName和targetNamespace成员用来描述wsdl:protType wsdl:service 和targetNameSpace生成WebService中的WSDL文件          @SOAPBinding是一个绑定的annotation 用来说明网络协议和格式        @SOAPBinding是一个用来描述SOAP格式和RPC的协议的绑定Annotation         @WebMethod Annotation的operationName成员描述了wsdl:operation 而且它的操作描述了WSDL文件中的SOAPAction头部 这是客户端必须要放入到SOAPHeader中的数值 SOAP 中的一种约束         @WebParam Annotation的partName成员描述了WSDL文档中的wsdl:part         @WebResult Annotation的partName成员描述了wsdl:part用来返回WSDL文档的值         第二步 创建webservice接口及实现类(下面为Java代码)        创建HelloWorld接口        View Code        @WebServicepublic interface HelloWorld         @WebMethod        String sayHi(@WebParam(name= text )String text)         @WebMethod        String sayHiToUser(User user)         @WebMethod        String[] SayHiToUserList(List<User> userList)         第三步 创建HelloWorldImpl实现类        View Code        @WebService(endpointInterface = demo spring service HelloWorld serviceName = HelloWorld )//endpointInterface表示该类就必须实现此接口所有方法 serviceName表示webservice的服务名称public class HelloWorldImpl implements HelloWorld         Map<Integer User> users = new LinkedHashMap<Integer User>()         @WebMethod        public String sayHi(String text)         return Hello + text;                @WebMethod        public String sayHiToUser(User user)         users put(users size() + user)         return Hello + user getName()                 @WebMethod        public String[] SayHiToUserList(List<User> userList)         String[] result = new String[userList size()];        int i = ;        for (User u : userList)         result[i] = Hello + u getName()         i++;                return result;                第四步 启动webserviceApp java 访//localhost: /helloWorld?WSDL        View Code        public class WebServiceApp         public static void main(String[] args)         System out println( web service start )         HelloWorldImpl implementor= new HelloWorldImpl()         String address=//localhost: /helloWorld ;        Endpoint publish(address implementor)         System out println( web service started )                 第五步 在web xml中加入cxf相应配置 内容如下         View Code        <! cxf start >  <! 用于加载applicationContext xml配置信息 >        <context param>        <param name>contextConfigLocation</param name>        <param value>WEB INF/classes/applicationContext xml</param value>        </context param>        <! 使用spring ContextLoaderListener 加载applicationContext xml >        <listener>        <listener class> sprntext ContextLoaderListener</listener class>        </listener>  <! 配置CXFServlet >        <servlet>        <servlet name>CXFServlet</servlet name>        <display name>CXF Servlet</display name>        <servlet class> apache cxf transport servlet CXFServlet</servlet class>        <load on startup> </load on startup>        </servlet>        <servlet mapping>        <servlet name>CXFServlet</servlet name>        <! url可自定义配置 用于CXFServlet请求地址拦截 访问会用到 >        <url pattern>/webservice/*</url pattern>        </servlet mapping>  <! cxf end >        第六步 在src中创建基本的applicationContext xml内容如下(作用 主要做webservice接口属性配置 通过web xml配置加载)        View Code        <?xml version= encoding= UTF ?><beans xmlns=         xmlns:xsi= instance         xmlns:jaxws=         xsi:schemaLocation=                 beans 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 /><! id:名称(随意配) implementor:指定接口具体实现类 address:随意配 >        <jaxws:endpoint id= helloWorld         implementor= demo spring service HelloWorldImpl         address= /HelloWorld /> <! WebService 客户端 spring 配置文件cxf与Spring集成 cxf里提供了一个工厂类 apache cxf jaxws JaxWsProxyFactoryBean 可以方便实现的调用WebService serviceClass属性是接口类 address是webService的路径在其他bean里如果要调用webservice 只要将client这个bean注入到需要使用的bean里 >        <bean id= client class= demo spring service HelloWorld         factory bean= clientFactory factory method= create />        <bean id= clientFactory         class= apache cxf jaxws JaxWsProxyFactoryBean >        <property name= serviceClass         value= demo spring service HelloWorld />        <property name= address         value=//localhost: /webservices/HelloWorld />        </bean></beans>        第七步 发布webservice到tomcat        Java代码 验证WSDL是否发布成功         如果项目发布放在/webapps/ROOT下时         访//IP地址 端口/webservices/applicationContent xml中配置的address?wsdl        //webservices对应web xml中的<url pattern>/webservices/*</url pattern>验证是否能正常看到xml格式的页面        第八步 测试        View Code        public class HelloWorldClient         public static void main(String[] args)         /**        * 简单的用ApplicationContext做测试的话 获得Spring中定义的Bean实例(对象) 可以用以下方法        * ClassPathXmlApplicationContext[只能读放在web info/classes目录下的配置文件]        */        ApplicationContext context = new ClassPathXmlApplicationContext(new String[] applicationContext xml )         HelloWorld client = (HelloWorld) context getBean( client )         User user = new User()         user setName( Tony )         user setDescription( test )         String str = client sayHiToUser(user)         System out println(str)         cha138/Article/program/Java/hx/201311/25827

相关参考

知识大全 使用 XML Web services 进行 Web 编程

使用XMLWebservices进行Web编程  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  X

知识大全 vs.net中web services入门

  Visual中的XMLWebservices入门  目录  简介用托管代码编写的XMLWebservices使用VisualStudio创建XMLWebservices使用VisualStudio

知识大全 c#动态调用Web Service

c#动态调用WebService  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! &nbs

知识大全 使用eclipse调用.net web service

  以前我用的开发框架都是net现在换成javaEE框架和linux平台还需要一段时间的学习有时在测试工作中需要实现一些功能但是又不会用java实现怎么办呢?这里有一个方法使用net的框架开发webs

知识大全 动态IP的Web service调用

动态IP的Webservice调用  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  系统架构需要使

知识大全 使用Axis开发Web Service[2]

Java高级开发:使用Axis开发WebService[2]  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看

知识大全 使用Axis开发Web Service[1]

Java高级开发:使用Axis开发WebService[1]  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看

知识大全 Spring集成XFire开发Web Service

Spring集成XFire开发WebService  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 

知识大全 如何通过Web Services上传和下载文件

如何通过WebServices上传和下载文件  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  随着

知识大全 实现异步调用Web Service,防止页面超时

实现异步调用WebService,防止页面超时  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  普