WebService--CXF以及CXF与Spring的整合(jaxws:server形式配置)

前言:好记性不如烂笔头,写博客的好处是,以前接触的东西即便忘记了,也可以从这里查找。

  Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services 。可以与Spring进行快速无缝的整合。灵活的部署,可以运行在Tomcat,Jboss,Jetty(内置),IBMWS,BeaWL上面。

  更多CXF介绍:http://www.ibm.com/developerworks/cn/education/java/j-cxf/

一、cxf发布服务的类

用两个不同的类发布应用:

  a. ServerFactoryBean   -- FacotryBean

  b. JaxWsServerFactoryBean(建议使用此类)

服务端与客户端类的使用映射关系如下图所示:

二、使用ServerFactoryBean类发布标准的webservice服务

  下载cxf安装包apache-cxf-2.4.2,里面lib下jar包以供项目所有。

1.服务端

a.新建web项目,加入cxf的jar包

b.定义webservice的接口

package com.wp.service;
import javax.jws.WebService;
@WebService
public interface HelloWs {
    public String sayHello(String name);
}

c.定义webservice接口的实现类

package com.wp.service;
public class HelloWsImpl implements HelloWs {
    @Override
    public String sayHello(String name) {
        return "hello" + name;
    }
}

d.发布webservice服务

package com.wp.service;
import org.apache.cxf.frontend.ServerFactoryBean;
public class Server {
    public static void main(String[] args) {
        ServerFactoryBean sfb = new ServerFactoryBean();
        //1.服务提供者实现的接口
        sfb.setServiceClass(HelloWs.class);
        //2.指定访问路径
        sfb.setAddress("http://localhost:9090/ws");
        //3.指定服务实现类
        sfb.setServiceBean(new HelloWsImpl());
        //4.发布
        sfb.create();
        System.out.println("发布成功...");
    }
}

2.客户端

a.使用wsdl2java生成客户端代码调用服务(之前有解释,这里就不做解释了)

b.使用ClientProxyFactoryBean类调用service服务

  客户端必须加入cxf的jar包

  浏览器访问http://localhost:9090/ws?wsdl ,查看service和operation

  1) 不同项目中调用

  

package com.wp.test;
import com.wp.client.HelloWs;
import com.wp.client.HelloWsPortType;
public class Client {
    public static void main(String[] args) {
        HelloWs hw = new HelloWs();
        HelloWsPortType h = hw.getHelloWsPort();
        String result = h.sayHello("小强");
        System.out.println(result);
    }
}

  1) 在同一项目中调用

package com.wp.service;
import org.apache.cxf.frontend.ClientProxyFactoryBean;
public class Client {
    public static void main(String[] args) {
        ClientProxyFactoryBean cfb = new ClientProxyFactoryBean();
        cfb.setAddress("http://localhost:9090/ws");
        cfb.setServiceClass(HelloWs.class);
        HelloWs hw = (HelloWs) cfb.create();
        System.out.println(hw.sayHello("明明"));
    }
}

类似的例子在apache-cxf-2.4.2安装包下的samples中,开发的时候可以查看

三、使用JaxWsServerFactoryBean类发布服务

1.服务端

package com.wp.service;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface HelloWs {
    public String sayHello(@WebParam(name="text") String text);
}
package com.wp.service;
import javax.jws.WebService;
@WebService(endpointInterface = "com.wp.service.HelloWs")
public class HelloWsImpl implements HelloWs {
    @Override
    public String sayHello(String name) {
        return "hello  " + name;
    }
}
package com.wp.service;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
public class Server {
    public static void main(String[] args) {
        JaxWsServerFactoryBean jsfb = new JaxWsServerFactoryBean();
        //1.服务提供者实现的接口
        jsfb.setServiceClass(HelloWs.class);
        //2.指定访问路径
        jsfb.setAddress("http://localhost:9090/ws");
        //3.指定服务实现类
        jsfb.setServiceBean(new HelloWsImpl());
        //jsfb.getInInterceptors().add(new LoggingInInterceptor());
        //jsfb.getOutInterceptors().add(new LoggingOutInterceptor());
        //4.发布
        jsfb.create();
        System.out.println("发布成功...");
    }
}

2.客户端

生成客户端代码:

a.在不同项目中

package com.wp.test;
import org.apache.cxf.frontend.ClientProxyFactoryBean;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.wp.client.HelloWs;
public class Client {
    public static void main(String[] args) {
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
       // factory.getInInterceptors().add(new LoggingInInterceptor());
       // factory.getOutInterceptors().add(new LoggingOutInterceptor());
        factory.setAddress("http://localhost:9090/ws");
        HelloWs client = factory.create(HelloWs.class);
        System.out.println(client.sayHello("World"));
    }
}

b.在同一项目中

package com.wp.service;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
public class Client {
    public static void main(String[] args) {
        JaxWsProxyFactoryBean bean = new JaxWsProxyFactoryBean();
        bean.setAddress("http://localhost:9090/ws");
        bean.setServiceClass(HelloWs.class);
        HelloWs hw = (HelloWs) bean.create();
        System.out.println(hw.sayHello("www"));
    }
}

四、spring和cxf的集成(MyEclipse)jaxws:server配置

  CXF原生支持spring,可以和Spring无缝集成。本例通过tomcat启动容器来启动cxf服务。

1.新建一个maven项目(写过一个随笔),并且加入jar包

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.wp.test</groupId>
    <artifactId>cxfmaven</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>cxfmaven Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <properties>
        <spring.version>3.2.5.RELEASE_BUNDLE</spring.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-asm</artifactId>
            <version>3.0.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>2.4.2</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>2.4.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>2.4.2</version>
        </dependency>
        <!-- Jetty is needed if you're using the CXFServlet -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>2.4.2</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>cxfmaven</finalName>
    </build>
</project>
pom.xml

2.让spring管理ServerFactoryBean

<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://cxf.apache.org/jaxws
    http://cxf.apache.org/schemas/jaxws.xsd
    ">
    <!-- 
        1 导入cxf发布的命名空间
            xmlns:jaxws="http://cxf.apache.org/jaxws"
            http://cxf.apache.org/jaxws
            http://cxf.apache.org/schemas/jaxws.xsd
        2 通过配置发布webservice服务
            2.1 发布简单的webservice服务(不需要定义服务器的接口 ,了解)
                 <jaxws:endpoint address="" implementor="">
                 </jaxws:endpoint>
            2.2 发布标准的webservice服务
                2.2.1 发布的地址  address
                2.2.2 发布的服务器实现的接口  serviceClass
                2.2.3 指定服务具体提供者(实现类对象)  jaxws:serviceBean
     -->
     <!-- 实例化一个JaxWsServerFactoryBean对象 -->
     <jaxws:server address="/helloWs"
       serviceClass="com.wp.service.HelloWs">
         <jaxws:serviceBean>
             <bean class="com.wp.service.HelloWsImpl"></bean>
         </jaxws:serviceBean>
     </jaxws:server>
</beans>
spring.xml

3.cxf集成到web容器中

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <display-name></display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>cxf</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <!-- 初始化CXFServlet -->
        <init-param>
            <param-name>config-location</param-name>
            <param-value>classpath:beans.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>cxf</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>
web.xml

4.启动web容器发布webservice服务

wsdl访问地址:http://localhost:8080/wsService/helloWs?wsdl

  1. 通过Eclipse中的Web Services Explorer进行测试;
  2. 建立一个客户端项目进行测试,跟上面一样。

 

转载于:https://www.cnblogs.com/zhanxiaoyun/p/6144651.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个比较常见的需求,下面是整合步骤: 1. 在Spring配置文件中导入CXF配置文件 在Spring配置文件中添加如下配置,导入CXF配置文件: ```xml <import resource="classpath:META-INF/cxf/cxf.xml"/> <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/> ``` 2. 配置Web Service服务端 在Spring配置文件中配置Web Service服务端,例如: ```xml <bean id="helloService" class="com.example.service.impl.HelloServiceImpl"/> <jaxws:endpoint id="helloServiceEndpoint" implementor="#helloService" address="/hello"/> ``` 其中,HelloServiceImpl是Web Service服务端的实现类,helloServiceEndpoint是服务端的访问地址。 3. 配置Web Service客户端 在Spring配置文件中配置Web Service客户端,例如: ```xml <jaxws:client id="helloServiceClient" serviceClass="com.example.service.HelloService" address="http://localhost:8080/myapp/hello"/> ``` 其中,HelloService是Web Service客户端的接口,helloServiceClient是客户端的访问地址。 4. 配置CXF Servlet 在web.xml配置CXF Servlet,例如: ```xml <servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <init-param> <param-name>config-location</param-name> <param-value>classpath:META-INF/cxf/cxf.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> ``` 其中,/services/*是Web Service服务端的访问地址。 至此,SSM框架整合CXF开发Web Service的步骤就完成了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值