星空网 > 软件开发 > Java

Spring与CXF

            最近项目中有用到Webservice,研究了Spring与CXF整合的官方文档(其实官方文档说的清楚了,建议大伙还是看官方文档,都是很简单英文单词),所以就有了下面的demo,相信大伙都能看懂

            用的主要架构为Maven + Spring + CXF + IDEA,废话就不多说了,先看下整个个项目结构

一、项目结构图

      Spring与CXF

二、服务端

   1.我们首先来编写服务端接口HelloWorld.java

package cn.starlin.springcxf;import javax.jws.WebService;/** * 定义服务接口 * on 2016/03/01 9:34. */@WebServicepublic interface HelloWorld {  public String sayHello(String str);}

  2.定义服务端接口实现类HelloWorldImpl

package cn.starlin.springcxf;import javax.jws.WebService;/** * 服务接口实现类 * on 2016/03/01 9:36. */@WebService(endpointInterface = "cn.starlin.springcxf.HelloWorld")public class HelloWorldImpl implements HelloWorld {  public String sayHello(String str) {    System.out.println("syaHello called");    return "Hello " + str;  }}

  3.声明servers的bean,创建cxf-servlet.

<beans ="http://www.springframework.org/schema/beans"    ="http://www.w3.org/2001/    ="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">  <!--由Spring管理-->  <bean id="hello" class="cn.starlin.springcxf.HelloWorldImpl" />  <jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" /></beans>

  4.配置web.

<web-app ="http://java.sun.com/     ="http://www.w3.org/2001/ version="2.5"     xsi:schemaLocation="http://java.sun.com/> <display-name>cxf</display-name> <servlet>  <description>Apache CXF Endpoint</description>  <display-name>cxf</display-name>  <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> <session-config>  <session-timeout>60</session-timeout> </session-config></web-app>

  5.启动Tomcat,访问http://localhost:8080/SpringCXF/,若能出现下图界面,就表示服务端成功了

Spring与CXF

 

三、客户端

  1.客户端 client-beans.

<??><beans ="http://www.springframework.org/schema/beans"    ="http://www.w3.org/2001/    ="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/schema/jaxws.xsd">  <bean id="client" class="cn.starlin.springcxf.HelloWorld"     factory-bean="clientFactory" factory-method="create"/>  <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">    <property name="serviceClass" value="cn.starlin.springcxf.HelloWorld"/>    <property name="address" value="http://localhost:8080/SpringCXF/HelloWorld"/>  </bean></beans>

  2.客户端代码HelloWorldClien.java

package cn.starlin.springcxf;import org.springframework.context.support.ClassPath/** * 客户端 * on 2016/03/01 10:53. */public class HelloWorldClient {  public static void main(String args[]) throws Exception {    ClassPath= new ClassPathnew String[] {"client-beans.});    HelloWorld client = (HelloWorld)context.getBean("client");//client需与client-beans.    String response = client.sayHello("starlin");    System.out.println("Response: " + response);    System.exit(0);  }}

  3.运行以下客户端代码,出现以下信息就表示成功了

Spring与CXF

同时服务端也会打印出相关信息

Spring与CXF

 

 

至此整合完成!!!!




原标题:Spring与CXF

关键词:Spring

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

君威国际货运代理:https://www.goluckyvip.com/tag/35012.html
君誉物流:https://www.goluckyvip.com/tag/35013.html
俊航兄弟跨境电商物流:https://www.goluckyvip.com/tag/35014.html
俊拓科技:https://www.goluckyvip.com/tag/35015.html
隽森供应链:https://www.goluckyvip.com/tag/35016.html
隽永东方:https://www.goluckyvip.com/tag/35017.html
皇帝的皇宫=:https://www.vstour.cn/a/363188.html
海南岛琼海市旅游景点 琼海市的旅游景点:https://www.vstour.cn/a/363189.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流