当前位置: 移动技术网 > IT编程>开发语言>Java > 基于CXF搭建webService的实例讲解

基于CXF搭建webService的实例讲解

2019年07月19日  | 移动技术网IT编程  | 我要评论

1.导入相关jar包,具体哪些包我记不太清了

2.在applicationcontext中加入相关配置信息,如下所示:

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" 
    xmlns:xsd="http://www.w3.org/2001/xmlschema" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jaxws="http://cxf.apache.org/jaxws" 
    xsi:schemalocation="http://www.springframework.org/schema/beans 
              http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
              http://www.springframework.org/schema/context 
              http://www.springframework.org/schema/context/spring-context-3.0.xsd 
              http://cxf.apache.org/jaxws 
              http://cxf.apache.org/schemas/jaxws.xsd"> 
 
    <context:component-scan base-package="com.cxf.bo"/> 
   
    <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="orderws" 
      implementor="com.cxf.spring.ws.orderwsimpl"//类所在地址或者#+对应bean的id
      address="/orderws" > //随意命名
      <jaxws:features> 
       <bean class="org.apache.cxf.feature.loggingfeature" /> 
      </jaxws:features> 
    </jaxws:endpoint> 
</beans>

3.在web.xml文件中加入:

<!-- cxf配置 -->
   <servlet> 
    <servlet-name>cxfservlet</servlet-name> 
    <servlet-class>org.apache.cxf.transport.servlet.cxfservlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
  </servlet> 
  <servlet-mapping> 
    <servlet-name>cxfservlet</servlet-name> 
    <url-pattern>/cxfservlet/*</url-pattern> 
  </servlet-mapping>

4.在service层加入:

@webservice
public interface orderws {
  @webmethod
  public order getorderbyid(int id);
}

5.若存在struts2,会发生冲突,则重写过滤器

5.1 写一个类extendstrutsfilter:

package com.nbu.retailer.filter;

import java.io.ioexception;

import javax.servlet.filterchain;
import javax.servlet.servletexception;
import javax.servlet.servletrequest;
import javax.servlet.servletresponse;
import javax.servlet.http.httpservletrequest;
import org.slf4j.logger; 
import org.slf4j.loggerfactory; 

import org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter;

public class extendstrutsfilter extends strutsprepareandexecutefilter{
  private static logger log = loggerfactory.getlogger(extendstrutsfilter.class); 
  @override 
  public void dofilter(servletrequest req, servletresponse res,filterchain chain) 
      throws ioexception, servletexception { 
      try { 
        httpservletrequest request = (httpservletrequest) req; 
        // 判断是否是向webservice发出的请求 
        if (request.getrequesturi().contains("/cxfservlet")) { 
           // 如果是来自向cxfservice发出的请求 
           chain.dofilter(req, res); 
         } 
         else { 
           // 默认action请求 
            super.dofilter(req, res, chain); 
         } 
       } 
      catch (exception e) { 
         log.error(e.getmessage()); 
         e.printstacktrace(); 
      } 
  } 

}

5.2 在web.xml中改变过滤器的地址:

<!-- struts2的过滤器-->
   <filter>
    <filter-name>struts2</filter-name>
    <!--
      <filter-class>org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter</filter-class>
    -->
    <!-- 自定义struts2过虑器类 用于解决struts2把cxf的请求当action处理的问题-->
    <filter-class>com.nbu.retailer.filter.extendstrutsfilter</filter-class>
  </filter>

5.3 注意,cxf的url和struts2的url不能相同。之前就这个问题困扰了我好久才发现的。

以上这篇基于cxf搭建webservice的实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网