基于CXF搭建webService永利澳门游戏网址304:,CXF搭建webService

基于CXF搭建webService,CXF搭建webService

1.导入相关jar包,具体如何包小编记不太清了

永利澳门游戏网址304,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无法平等。从前就以此主题材料压抑了自身好久才开掘的。

 

1.导入相关jar包,具体怎么包小编记不老聃了
2.在applicationContext中步向相关安插新闻,如下所示: beans…

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);
}

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注