Spring与CXF

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

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

一、项目结构图

      image

二、服务端

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

package cn.starlin.springcxf;

import javax.jws.WebService;

/**
 * 定义服务接口
 * on 2016/03/01 9:34.
 */

@WebService
public 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.xml文件

<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">

    <!--由Spring管理-->
    <bean id="hello" class="cn.starlin.springcxf.HelloWorldImpl" />

    <jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" />
</beans>

  4.配置web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
         http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <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/,若能出现下图界面,就表示服务端成功了

image

 

三、客户端

  1.客户端 client-beans.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/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.ClassPathXmlApplicationContext;

/**
 * 客户端
 * on 2016/03/01 10:53.
 */
public class HelloWorldClient {
    public static void main(String args[]) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"client-beans.xml"});
        HelloWorld client = (HelloWorld)context.getBean("client");//client需与client-beans.xml中的id一致
        String response = client.sayHello("starlin");
        System.out.println("Response: " + response);
        System.exit(0);

    }
}

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

image

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

image

 

 

至此整合完成!!!!

转载于:https://www.cnblogs.com/starlin/p/5230822.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值