SOA 下实现分布式 调用 cxf+ webService +动态调用

   近期项目间隙 自学了  webservice   一下 是我写的  一个demo   

    首先我们在web.xml 里配置如下

     

  1. <servlet>  
  2.     <servlet-name>CXFServlet</servlet-name>  
  3.     <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
  4.     <load-on-startup>1</load-on-startup>  
  5.   </servlet>  
  6.   <servlet-mapping>  
  7.     <servlet-name>CXFServlet</servlet-name>  
  8.     <url-pattern>/services/*</url-pattern>  //拦截qinqiu
  9.   </servlet-mapping>  

我用的  是maven项目 一下 pom.xml 引入一下依赖 

<dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>2.7.14</version>
            <exclusions>
                <exclusion>
                    <groupId>asm</groupId>
                    <artifactId>asm</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.xml.bind</groupId>
                    <artifactId>jaxb-impl</artifactId>
                </exclusion>
 
            </exclusions>
        </dependency>
 
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>2.7.14</version>
        </dependency>
 
spring—cxf.xml 配置如下
<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:task="http://www.springframework.org/schema/task" xmlns:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                        http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.2.xsd
                        http://www.springframework.org/schema/jee
                        http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
                        http://www.springframework.org/schema/task
                        http://www.springframework.org/schema/task/spring-task-3.2.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="lMSWebService"
        implementor="wenqiang_web.wenqiang_mavenWeb.cn.wenqiang.service.impl.UserWebServiceimpl"
        address="/lMSWebService">
    </jaxws:endpoint>
</beans>

 

接口列子
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
 
import wenqiang_web.wenqiang_mavenWeb.cn.wenqiang.emty.User;
import wenqiang_web.wenqiang_mavenWeb.cn.wenqiang.util.PageData;
@WebService(targetNamespace="http://impl.service.wenqiang.cn.wenqiang_mavenWeb.wenqiang_web/")
@SOAPBinding(style = Style.RPC)
public interface UserWebService {
    String getUserByNameAndPwd(@WebParam(name = "pd")PageData pd);    
}
接口实现类
public class UserWebServiceimpl   implements UserWebService {
    @Autowired
    UserMapper  usermapper;
      public String getUserByNameAndPwd(@WebParam(name = "pd")PageData pd) {
          Gson gson = new Gson();
          String fenInformation = gson.toJson(usermapper.selectUserLogin(pd));
        return fenInformation;
    }
}
 
动态调用  工具类
 
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.log4j.Logger;
import org.springframework.util.StringUtils;
 
/**
 * 
 * 使用cxf 调用webservice 接口
 * 
 * @author chenj
 *
 */
public class CxfInvokeUtil {
 
    static Logger logger = Logger.getLogger(CxfInvokeUtil.class);
    
    /**
     * 
     * 调用webservice 接口
     * 
     * @param wsdlUrl  wsdl 地址
     *  
     * @param method  调用方法名
     * 
     * @param params  接口传入参数
     * 
     * @return
     * 
     */
    public static synchronized Object[] invoke(String wsdlUrl,String method,Object... params){
        
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); 
        org.apache.cxf.endpoint.Client client = dcf.createClient(wsdlUrl); 
        
        Object[] objects = null;
        
        if(StringUtils.isEmpty(wsdlUrl)){
    
            logger.error("cxf 调用webservice 参数缺失:wsdl url 未传入");
            return objects;
        }
        
        if(StringUtils.isEmpty(method)){
            
            logger.error("cxf 调用webservice 执行方法名缺失:method 未传入");
            return objects;
        }
        
        try {
 
               objects=client.invoke(method,params);
 
            } catch (Exception e) {
                e.printStackTrace();
                logger.error("cxf 调用webservice 执行错误:",e);
 
            }
        
        return objects;
    }
}
 

 
动态调用测试类
import java.net.URLEncoder;
import java.util.Date;
 
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
 
 
 
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
 
import wenqiang_web.wenqiang_mavenWeb.cn.wenqiang.service.UserWebService;
import wenqiang_web.wenqiang_mavenWeb.cn.wenqiang.util.CxfInvokeUtil;
 
import com.google.gson.Gson;
import com.google.gson.JsonObject;
 
 
 
public class MyTestClient {
    public static void main(String[] args) throws Exception {
  
    String wsdlUrl="http://localhost:8070/wenqiang_mavenWeb/Webservices/lMSWebService?wsdl";
    String id="1";
    Object[] obs = CxfInvokeUtil.invoke(wsdlUrl,"selectByPrimaryKey",id);
    if(obs != null && obs.length > 0){
        String result = (String)obs[0];
        System.out.print(result);
        
        
        
    }

      

          如有不懂  可以和 本人联系  QQ:344436738   

posted on 2017-05-03 11:24  AnkangWenqiang  阅读(2959)  评论(1编辑  收藏  举报

导航