apache CXF入门-----简单的案例开发

下载

·首先我们先从官网:cxf.apache.org下载所需的jar包

  1. Apache CXF = Celtix + Xfire

     2.支持多种协议:(1)SOAP1.1,1.2

                                 (2)XML/HTTP

                                 (3)CORBA(Common Object Request Broker Architecture公共对象请求代理体系结构,早期语言使用的                                                WS。C,c++,C#)

                                 (4)并可以与Spring进行快速无缝的整合

                                 (5)灵活的部署:可以运行在Tomcat,Jboss,Jetty(内置),IBMWS,BeaWL上面。

入门案例之(服务端的开发)

1.我们先创建一个动态的WEB项目

2.导入CXF所需的架包

3.在我们的web.xml里面配置cxf框架提供的servlet

  <servlet>
      <servlet-name>cxf</servlet-name>
      <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
      <!-- 通过初始化参数指定CXF框架的配置文件位置 -->
      <init-param>
          <param-name>config-location</param-name>
          <param-value>classpath:cxf.xml</param-value>
      </init-param>
  </servlet>
  <servlet-mapping>
      <servlet-name>cxf</servlet-name>
      <url-pattern>/service/*</url-pattern>
  </servlet-mapping>

4. 在类路径下提供cxf.xml   我在src下面创建cfx文件

<?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"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
                    http://www.springframework.org/schema/beans/spring-beans.xsd
                    http://cxf.apache.org/bindings/soap 
                    http://cxf.apache.org/schemas/configuration/soap.xsd
                    http://cxf.apache.org/jaxws 
                    http://cxf.apache.org/schemas/jaxws.xsd">
    <!-- 引入CXF Bean定义如下,早期的版本中使用 -->
    <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" />

5.写一个接口类和实现类

import javax.jws.WebService;

这里要加上@WebService这个注解

@WebService
public interface HellowService {
    public String sayHello(String name);
}

实现类

public class HellowServiceimpl implements HellowService {

    @Override
    public String sayHello(String name) {
        // TODO Auto-generated method stub
      System.out.println("基于CXF开发的服务端sayHellow方法被调用。。。");
      return "hellow"+name;
              
    }

}

6.在我们的cxf里面注册我们的服务

<bean id="hellowService" class="com.yinhe.service.HellowServiceimpl"></bean>
    <jaxws:server id="myService" address="/cxfService">
         <jaxws:serviceBean>
                <ref bean="hellowService"/>
         </jaxws:serviceBean>
    </jaxws:server>

入门案例之(客户端的开发)

1.创建新的项目

 2.使用jdk提供的wsimport命令生成本地代码完成调用

打开cmd 在里面如如如下 下载文件

D:\study\java>wsimport -s . http://localhost:8080/ibos_crm/service/customer?wsdl

2.将下载的文件中的接口文件放入项目中

并修改接口中的注解  改为@XmlSeeAlso({})

3.创建cxf.xml文件  在里面注册cfx客户代理对象

<jaxws:client id="myClient"
     address="http://localhost:8080/ibos_crm/service/customer"
     serviceClass="com.yinhe.clicen.HelloService">
    
    </jaxws:client>

4.读取spring配置文件,创建spring工厂,从工厂中获取代理对象,实现远程调用

public class App {
     
    public static void main(String[] args) {
        ApplicationContext app=new ClassPathXmlApplicationContext("cxf.xml");
        ICustomerService service=(ICustomerService) app.getBean("myClient");
        String ret=service.sayhellow("text");
        System.out.println(ret);
        
    }
}

 

 

                   

猜你喜欢

转载自blog.csdn.net/qq_41326048/article/details/83383335