CXF发布webService

来源:互联网 发布:淘宝店铺的模板怎么做 编辑:程序博客网 时间:2024/06/06 20:23

Web 服务标准支持:CXF 支持以下 Web 服务标准:

    Java API for XML Web Services (JAX-WS)    SOAP    Web 服务描述语言(Web Services Description Language ,WSDL)    消息传输优化机制(Message Transmission Optimization Mechanism,MTOM)    WS-Basic Profile    WS-Addressing    WS-Policy    WS-ReliableMessaging    WS-Security

cxf:发布WebService服务的方式:
1、下载cxf,并将其lib目录下依赖的jar包添加到工程里面,若在maven项目中,则需要添加 如下依赖

        <dependency>            <groupId>org.apache.cxf</groupId>            <artifactId>cxf-rt-transports-http-jetty</artifactId>            <version>2.7.14</version>        </dependency>        JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();        factory.setServiceClass(HelloWorld.class);        factory.setAddress("http://localhost:8080/HelloWorld");        Server server = factory.create();        server.start();另一种发布webService的方式:        JaxWsServerFactoryBean soapFactoryBean = new                JaxWsServerFactoryBean();        soapFactoryBean.getInInterceptors().add(new                LoggingInInterceptor());        soapFactoryBean.getOutInterceptors().add(new                LoggingOutInterceptor());    // 注意这里是实现类不是接口        soapFactoryBean.setServiceClass(HelloWorld.class);        soapFactoryBean.setAddress("http://127.0.0.1:8080/helloService");        soapFactoryBean.create();    此处添加了日志拦截器(拦截器是 CXF 的一项扩展功能,CXF 提供了很多拦截器实现,你也可以自己实现一种拦    截器),这样可以在 Web 服务端发送和接收消息时输出信息。

当然如上的方式和利用原生jax-ws的注解方式发布服务并没有什么优势。
使用原生的java注解也可以实现webService的发布,但是如果需要将webService服务和项目放到一个web容器中,那么就需要用到例如cxf这样的工具了。

cxf 整合 spring:
1、添加pom依赖

        <dependency>            <groupId>org.apache.cxf</groupId>            <artifactId>cxf-rt-frontend-jaxws</artifactId>            <version>2.7.14</version>        </dependency>        <dependency>            <groupId>org.apache.cxf</groupId>            <artifactId>cxf-rt-transports-http</artifactId>            <version>2.7.14</version>        </dependency>注意:整合spring的时候,spring的相关依赖需要在3以上。例如:        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context</artifactId>            <version>3.2.18.RELEASE</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-core</artifactId>            <version>3.2.18.RELEASE</version>        </dependency>

2、web.xml文件中注册cxf的servelt

    <servlet>      <servlet-name>CXFServlet</servlet-name>      <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>      </servlet>      <servlet-mapping>      <servlet-name>CXFServlet</servlet-name>      <url-pattern>/webservice/*</url-pattern>      </servlet-mapping>  

3、在spring的配置文件中添加bean:

    <?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/schemas/jaxws.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" />        <jaxws:endpoint id="helloWorld"                    implementor="com.xxx.xxx.webService.HelloWorld"                    address="/helloWorld" />    </beans>发布webService的java类,一般基于接口和实现的方式,在接口中使用WebService注解,表明这是一个发布webService的接口,默认情况下所有的接口方法都是对外发布的webService,

4、生成的wsdl访问路径:
项目路径+cxf的servlet路径+spring的bean的路径+?wsdl

5、常见问题:
No bean named ‘cxf’ is defined
主要的原因是cxf对象实例初始化顺序晚于CXFServlet, 故需要将其spring-cxf-service.xml的初始化顺序从原来的dispatcherServlet, 提前至中的初始化参数,在web.xml文件加载spring的配置文件。

contextConfigLocation
classpath:conf/spring*.xml

原创粉丝点击