jeeCmsV7-src 源码解析之九(jeeCms整合webservice)

第一步:在jeeCms .添加jar包


第二步:增加webservice相关配置。

web.xml

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			<!-- 数据库相关配置并加载了hbm.xml文件 (Spring)-->
			/WEB-INF/config/application-context.xml
			<!-- 数据缓存相关配置 -->
			/WEB-INF/config/cache-context.xml
			<!-- 验证码相关配置 --> 
			/WEB-INF/config/captcha-context.xml
			 <!-- 图片,密码,文件上传等配置 --> 
			/WEB-INF/config/jeecms/jeecore-context.xml
			<!-- spring bean相关配置,如dao层的配置、图片处理,密码加密 ,freemaker的使用-->
			/WEB-INF/config/jeecms/jeecms-context.xml
			<!-- 配置权限管理shiro-context.xml框架 -->
			/WEB-INF/config/shiro-context.xml
			<!-- 配置其它插件文件,暂时没有 -->
			/WEB-INF/config/plug/**/*-context.xml
			<!-- 配置定时任务 -->
			/WEB-INF/config/quartz-task.xml
			<!-- 配置webservice -->
		  <span style="color:#ff6666;">      /WEB-INF/config/webservice/cxf-service.xml</span>
		</param-value>
	</context-param>

	<!-- CXF webservice -->
    <servlet>  
		<servlet-name>CXFServlet</servlet-name>  
		<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
	<load-on-startup>2</load-on-startup>  
  	</servlet>
  	<servlet-mapping>  
		<servlet-name>CXFServlet</servlet-name>  
		<url-pattern>/ws/*</url-pattern>  
 	</servlet-mapping>


cxf-service.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:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
	http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
	http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
	http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd">    
	
	<http-conf:conduit name="*.http-conduit">     
		<http-conf:client ConnectionTimeout="60000" ReceiveTimeout="60000"/>    
	</http-conf:conduit>
	<!-- 这两个xml文件来源cxf jar包 -->
	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
	
	<!-- 这个文件中用来配置webservice服务器 -->
	<import resource="action-cxf.xml" />

</beans>


action-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:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee"
	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://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
	http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
	http://cxf.apache.org/jaxws      
	http://cxf.apache.org/schemas/jaxws.xsd">
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
	<!-- 
	<jaxws:endpoint id="ActPromWebService" implementor="#bizActPromWebService" address="/ActPromWebService" />
	 -->
</beans>


第三步 创奸webService 接口类,用来存放调用webService 的方法

package com.jeecms.cms.action.directive;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

@WebService(targetNamespace="http://webservice.cus.biz.cbp.cj.com/")
public interface countInfoInterfaceNpl {
	
	public static final String SERVICE_NAME = "FindAllCustWebSerrvice";

	/**
	 * 根据客户ID查询客户的详细信息
	 * 
	 * @param traT
	 * @param custB
	 * @return
	 */
	@WebMethod
	String findByIdCustB(@WebParam(name = "partnerId") String partnerId, @WebParam(name = "traT") String traT, @WebParam(name = "custB") String custB,
			@WebParam(name = "orderId") String orderId,@WebParam(name = "verifyCode") String verifyCode);

}
第四步 java类中调用webservice 

package com.jeecms.cms.action.directive;

import static com.jeecms.common.web.freemarker.DirectiveUtils.OUT_BEAN;
import static freemarker.template.ObjectWrapper.DEFAULT_WRAPPER;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import javax.jws.WebService;

import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import com.jeecms.common.web.freemarker.DirectiveUtils;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
 
/**
 * 总计信息
 */
public class CountInfoNpl implements TemplateDirectiveModel {

	@SuppressWarnings("unchecked")
	public void execute(Environment env, Map params, TemplateModel[] loopVars,
			TemplateDirectiveBody body) throws TemplateException, IOException {
		try {
		int i=1;
		if(i==1){
			 JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
             factory.getInInterceptors().add(new LoggingInInterceptor());
             factory.getOutInterceptors().add(new LoggingOutInterceptor());
             factory.setServiceClass(countInfoInterfaceNpl.class);//调用的接口类
             factory.setAddress("http://127.0.0.1:8060/ws/FindAllCustWebSerrvice");
             Object cInstance = factory.create();
             Method invokeMethod = null;
            //循环接口类的所有方法,并与实际调用的方法比较,若不存在,则给出错误信息
             for(Method m : (countInfoInterfaceNpl.class).getDeclaredMethods()){
                 if(!m.getName().equalsIgnoreCase("")){
                     invokeMethod = m;
                     break;
                 }
             }
             if(invokeMethod == null)
                 throw new Exception("ERROR:method not found");
            //为webService添加参数
     		Object[] params1 = new Object[5];
     		params1[0] = null;
     		params1[1] = "aaaa";
     		params1[2] = "bbnn";
     		params1[3] = null;
     		params1[4] = null;
     		//调用webService接口,并用CustNpl接收返回值
			CustBNpl res = (CustBNpl) invokeMethod.invoke(cInstance, params1);
            System.out.println("aaa");
		}else{
			Map<String,String> cmsVoteInfo=new HashMap<String,String>();
			cmsVoteInfo.put("publicInfo", "加薪宝涨息");
			cmsVoteInfo.put("totalCaptial","20000001");
			cmsVoteInfo.put("goldCaptial","5555555");
			cmsVoteInfo.put("getMoney","6666666");
			
			Map<String, TemplateModel> paramWrap = new HashMap<String, TemplateModel>(
					params);
			paramWrap.put(OUT_BEAN, DEFAULT_WRAPPER.wrap(cmsVoteInfo));
			Map<String, TemplateModel> origMap = DirectiveUtils.addParamsToVariable(env, paramWrap);
			body.render(env.getOut());
			DirectiveUtils.removeParamsFromVariable(env, paramWrap, origMap);
		}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}



猜你喜欢

转载自blog.csdn.net/nipanlong001/article/details/52450499