CXF学习笔记---让通过参数传递数据

整整折腾了3天终于通过CXF进行参数传递了。CXF的文档和sample都是存在问题的。这么一些简单的常用内容,硬是找不着。opensource的弊病。
目地:
通过webservice传递值以及错误信息。true:取result值,false:取errorNum和errorMsg

【Server】
1、Interface
Java代码 复制代码
  1. @WebService  
  2. public interface DatasetExcute {   
  3. //  List<TableDataInfo> excuteSql(String sql);   
  4.     String excuteSqlWithReturnXml(String sql);   
  5. //  Document excuteSqlWithReturnDoc(String sql);   
  6.     public boolean excuteSqlWithReturnDocAndMsg(   
  7.             @WebParam(name = "excuteSql",  header = true,mode = WebParam.Mode.IN)   
  8.             String sql, @WebParam(name = "excuteResult", targetNamespace = "",mode = WebParam.Mode.OUT)   
  9.             javax.xml.ws.Holder<java.lang.String> result, @WebParam(name = "errorNumber", targetNamespace = "", mode = WebParam.Mode.OUT)   
  10.             javax.xml.ws.Holder<java.lang.String> errorNumber, @WebParam(name = "errorMsg", targetNamespace = "", mode = WebParam.Mode.OUT)   
  11.             javax.xml.ws.Holder<java.lang.String> errorMsg);   
  12. }  
@WebService
public interface DatasetExcute {
//	List<TableDataInfo> excuteSql(String sql);
	String excuteSqlWithReturnXml(String sql);
//	Document excuteSqlWithReturnDoc(String sql);
	public boolean excuteSqlWithReturnDocAndMsg(
			@WebParam(name = "excuteSql",  header = true,mode = WebParam.Mode.IN)
			String sql, @WebParam(name = "excuteResult", targetNamespace = "",mode = WebParam.Mode.OUT)
			javax.xml.ws.Holder<java.lang.String> result, @WebParam(name = "errorNumber", targetNamespace = "", mode = WebParam.Mode.OUT)
			javax.xml.ws.Holder<java.lang.String> errorNumber, @WebParam(name = "errorMsg", targetNamespace = "", mode = WebParam.Mode.OUT)
			javax.xml.ws.Holder<java.lang.String> errorMsg);
}


2.impletement文件
Java代码 复制代码
  1. @WebService(endpointInterface = "com.tnt.mms.webservice.DatasetExcute")   
  2. public class DatasetExcuteImpl implements DatasetExcute {   
  3.     private NativeDAO nativeDAO;   
  4.   
  5.     public void setNativeDAO(NativeDAO nativeDAO) {   
  6.         this.nativeDAO = nativeDAO;   
  7.     }   
  8.   
  9.     //   
  10.     // public List<TableDataInfo> excuteSql(String sql) {   
  11.     //           
  12.     //   
  13.     // List<TableDataInfo> ret=nativeDAO.bulkOperationWithReturnAndFields(sql);   
  14.     // return ret;   
  15.     //   
  16.     // }   
  17.   
  18.     public String excuteSqlWithReturnXml(String sql) {   
  19.         String xmlRet = nativeDAO.bulkOperationWithReturnXml(sql);   
  20.         return xmlRet;   
  21.     }   
  22.   
  23.     public Document excuteSqlWithReturnDoc(String sql) {   
  24.         Document xmlRet = nativeDAO.bulkOperationWithReturnDoc(sql);   
  25.         return xmlRet;   
  26.     }   
  27.   
  28.     public boolean excuteSqlWithReturnDocAndMsg(String sql,   
  29.             javax.xml.ws.Holder<java.lang.String> result,   
  30.             javax.xml.ws.Holder<java.lang.String> errorNumber,   
  31.             javax.xml.ws.Holder<java.lang.String> errorMsg) {   
  32.         boolean blnReturn = false;   
  33.         try {   
  34.             result.value = nativeDAO.bulkOperationWithReturnXml(sql);   
  35.   
  36.             blnReturn = true;   
  37.   
  38.         } catch (Exception e) {   
  39.             errorNumber.value = "ES0001";   
  40.             errorMsg.value = "Database Error!";   
  41.         }   
  42.         return blnReturn;   
  43.     }   
  44.   
  45. }  
@WebService(endpointInterface = "com.tnt.mms.webservice.DatasetExcute")
public class DatasetExcuteImpl implements DatasetExcute {
	private NativeDAO nativeDAO;

	public void setNativeDAO(NativeDAO nativeDAO) {
		this.nativeDAO = nativeDAO;
	}

	//
	// public List<TableDataInfo> excuteSql(String sql) {
	//		  
	//
	// List<TableDataInfo> ret=nativeDAO.bulkOperationWithReturnAndFields(sql);
	// return ret;
	//
	// }

	public String excuteSqlWithReturnXml(String sql) {
		String xmlRet = nativeDAO.bulkOperationWithReturnXml(sql);
		return xmlRet;
	}

	public Document excuteSqlWithReturnDoc(String sql) {
		Document xmlRet = nativeDAO.bulkOperationWithReturnDoc(sql);
		return xmlRet;
	}

	public boolean excuteSqlWithReturnDocAndMsg(String sql,
			javax.xml.ws.Holder<java.lang.String> result,
			javax.xml.ws.Holder<java.lang.String> errorNumber,
			javax.xml.ws.Holder<java.lang.String> errorMsg) {
		boolean blnReturn = false;
		try {
			result.value = nativeDAO.bulkOperationWithReturnXml(sql);

			blnReturn = true;

		} catch (Exception e) {
			errorNumber.value = "ES0001";
			errorMsg.value = "Database Error!";
		}
		return blnReturn;
	}

}



3、描述文件
Java代码 复制代码
  1. <jaxws:endpoint id="DatasetExcute"  
  2.     implementorClass="com.tnt.mms.webservice.DatasetExcuteImpl"  
  3.     implementor="#DatasetExcuteImpl" address="/DatasetExcute">   
  4.     <!-- 用参数传递,就不要用aegis   
  5.     <jaxws:serviceFactory>   
  6.         <ref bean="jaxws-and-aegis-service-factory" />   
  7.     </jaxws:serviceFactory>-->   
  8. </jaxws:endpoint>  
	<jaxws:endpoint id="DatasetExcute"
		implementorClass="com.tnt.mms.webservice.DatasetExcuteImpl"
		implementor="#DatasetExcuteImpl" address="/DatasetExcute">
		<!-- 用参数传递,就不要用aegis
		<jaxws:serviceFactory>
			<ref bean="jaxws-and-aegis-service-factory" />
		</jaxws:serviceFactory>-->
	</jaxws:endpoint>


【client test】
Java代码 复制代码
  1. public final class Client_DbExcute {   
  2.   
  3.     private Client_DbExcute() {   
  4.     }   
  5.   
  6.     public static void main(String args[]) throws Exception {   
  7.         // START SNIPPET: client   
  8.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(   
  9.                 new String[] { "com/tnt/mms/webservice/client/client-DbExcute.xml" });   
  10.         try {   
  11.             DatasetExcute client = (DatasetExcute) context.getBean("client");   
  12.             // List<TableDataInfo> list=null;   
  13.             // list = client.excuteSql("select* from vendor");   
  14.             // String list = client.excuteSqlWithReturnXml("select* from vendor   
  15.             // where vendorID like '111%'");   
  16.             // Document list = client   
  17.             // .excuteSqlWithReturnDoc("select* from vendor where vendorID like   
  18.             // '111%'");.   
  19.             StringBuffer sql = new StringBuffer();   
  20.             sql   
  21.                     .append("if object_id('tempdb..#UOM150232076') is not null begin");   
  22.             sql.append(" drop table     #UOM150232076    ");   
  23.             sql.append(" end");   
  24.   
  25.             sql.append("  begin tran");   
  26.   
  27.             sql.append(" select distinct itemid, pack, EngUOM as UOM,");   
  28.             sql   
  29.                     .append("coalesce(ManualWeight,0) as ManualWeight, coalesce(GST,0) as GST,");   
  30.             sql   
  31.                     .append("        ' ' as flag, getdate() as chgtime, '754410' as chgby");   
  32.             sql.append(" into #UOM150232076");   
  33.             sql.append("  from [tntdb150].RIS_test.dbo.Pos");   
  34.             sql.append("  where itemid = '663247'");   
  35.             sql.append(" order by pack");   
  36.   
  37.             sql.append(" if @@ROWCOUNT=0");   
  38.             sql   
  39.                     .append("  insert into #UOM150232076 (  itemid , pack          ,  UOM,");   
  40.             sql.append("ManualWeight, GST, flag,   chgtime,      chgby)");   
  41.             sql.append("           values('663247', 1000 * 0 + 1.0, 'ea',");   
  42.             sql.append("0,   0,  ' ', getdate(), '754410')");   
  43.   
  44.             sql.append(" select * from #UOM150232076");   
  45.   
  46.             sql.append(" if @@error <> 0");   
  47.             sql.append("    rollback tran");   
  48.             sql.append(" else");   
  49.             sql.append("   commit tran");   
  50.   
  51. //          Document list = client   
  52. //                  .excuteSqlWithReturnDoc("select* from vendor where vendorID='10118'");   
  53.            
  54.                
  55. //          Document list = client   
  56. //          .excuteSqlWithReturnDoc(sql.toString());   
  57. //          list.getFirstChild().getTextContent();   
  58. //          XmlUtil.save(list, "c:/test.xml");   
  59.                
  60. //          String list1= "";   
  61. //          String  errorNumber="", errorMsg = "";   
  62.             Holder<String> list1=new Holder<String>();   
  63.             Holder<String> errorNumber=new Holder<String>();   
  64.             Holder<String> errorMsg=new Holder<String>();   
  65.            
  66.         boolean ret=client.excuteSqlWithReturnDocAndMsg("select * from vendor where vendorID like '111%'", list1, errorNumber, errorMsg);   
  67.         if(ret)   
  68.         {   
  69.             System.out.println("result: "  
  70.                     + list1+":"+list1.value);   
  71.             XmlUtil.writeXml(list1.value, "c:/ls.xml");   
  72.         }   
  73.         else  
  74.         {   
  75.             System.out.println("Error: "  
  76.                     + errorNumber.value+":"+errorMsg.value);   
  77.         }   
  78.                
  79.             System.exit(0);   
  80.             // END SNIPPET: client   
  81.         } catch (Exception e) {   
  82.             e.printStackTrace();   
  83.             System.out.println("error: " + e.getMessage());   
  84.         }   
  85.   
  86.     }   
  87. }  
public final class Client_DbExcute {

	private Client_DbExcute() {
	}

	public static void main(String args[]) throws Exception {
		// START SNIPPET: client
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { "com/tnt/mms/webservice/client/client-DbExcute.xml" });
		try {
			DatasetExcute client = (DatasetExcute) context.getBean("client");
			// List<TableDataInfo> list=null;
			// list = client.excuteSql("select* from vendor");
			// String list = client.excuteSqlWithReturnXml("select* from vendor
			// where vendorID like '111%'");
			// Document list = client
			// .excuteSqlWithReturnDoc("select* from vendor where vendorID like
			// '111%'");.
			StringBuffer sql = new StringBuffer();
			sql
					.append("if object_id('tempdb..#UOM150232076') is not null begin");
			sql.append(" drop table 	#UOM150232076    ");
			sql.append(" end");

			sql.append("  begin tran");

			sql.append(" select distinct itemid, pack, EngUOM as UOM,");
			sql
					.append("coalesce(ManualWeight,0) as ManualWeight, coalesce(GST,0) as GST,");
			sql
					.append("        ' ' as flag, getdate() as chgtime, '754410' as chgby");
			sql.append(" into #UOM150232076");
			sql.append("  from [tntdb150].RIS_test.dbo.Pos");
			sql.append("  where itemid = '663247'");
			sql.append(" order by pack");

			sql.append(" if @@ROWCOUNT=0");
			sql
					.append("  insert into #UOM150232076 (  itemid , pack          ,  UOM,");
			sql.append("ManualWeight, GST, flag,   chgtime,      chgby)");
			sql.append("           values('663247', 1000 * 0 + 1.0, 'ea',");
			sql.append("0,   0,  ' ', getdate(), '754410')");

			sql.append(" select * from #UOM150232076");

			sql.append(" if @@error <> 0");
			sql.append("    rollback tran");
			sql.append(" else");
			sql.append("   commit tran");

//			Document list = client
//					.excuteSqlWithReturnDoc("select* from vendor where vendorID='10118'");
		
	        
//			Document list = client
//			.excuteSqlWithReturnDoc(sql.toString());
//			list.getFirstChild().getTextContent();
//			XmlUtil.save(list, "c:/test.xml");
			
//			String list1= "";
//			String  errorNumber="", errorMsg = "";
			Holder<String> list1=new Holder<String>();
			Holder<String> errorNumber=new Holder<String>();
			Holder<String> errorMsg=new Holder<String>();
		
		boolean ret=client.excuteSqlWithReturnDocAndMsg("select * from vendor where vendorID like '111%'", list1, errorNumber, errorMsg);
		if(ret)
		{
			System.out.println("result: "
					+ list1+":"+list1.value);
			XmlUtil.writeXml(list1.value, "c:/ls.xml");
		}
		else
		{
			System.out.println("Error: "
					+ errorNumber.value+":"+errorMsg.value);
		}
			
			System.exit(0);
			// END SNIPPET: client
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("error: " + e.getMessage());
		}

	}
}


2.配置文件
Java代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <!--   
  3.     Licensed to the Apache Software Foundation (ASF) under one   
  4.     or more contributor license agreements. See the NOTICE file   
  5.     distributed with this work for additional information   
  6.     regarding copyright ownership. The ASF licenses this file   
  7.     to you under the Apache License, Version 2.0 (the   
  8.     "License"); you may not use this file except in compliance   
  9.     with the License. You may obtain a copy of the License at   
  10.        
  11.     http://www.apache.org/licenses/LICENSE-2.0   
  12.        
  13.     Unless required by applicable law or agreed to in writing,   
  14.     software distributed under the License is distributed on an   
  15.     "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY   
  16.     KIND, either express or implied. See the License for the   
  17.     specific language governing permissions and limitations   
  18.     under the License.   
  19. -->   
  20. <!-- START SNIPPET: beans -->   
  21. <beans xmlns="http://www.springframework.org/schema/beans"  
  22.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  23.     xmlns:jaxws="http://cxf.apache.org/jaxws"  
  24.     xsi:schemaLocation="   
  25. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd   
  26. http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">   
  27.     <!-- Configure CXF to use Aegis data binding instead of JAXB -->   
  28.     <bean id="aegisBean"  
  29.         class="org.apache.cxf.aegis.databinding.AegisDatabinding"  
  30.         scope="prototype" />   
  31.     <bean id="jaxwsAndAegisServiceFactory"  
  32.         class="org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean"  
  33.         scope="prototype">   
  34.         <property name="dataBinding" ref="aegisBean" />   
  35.         <property name="serviceConfigurations">   
  36.             <list>   
  37.                 <bean   
  38.                     class="org.apache.cxf.jaxws.support.JaxWsServiceConfiguration" />   
  39.                 <bean   
  40.                     class="org.apache.cxf.aegis.databinding.AegisServiceConfiguration" />   
  41.                 <bean   
  42.                     class="org.apache.cxf.service.factory.DefaultServiceConfiguration" />   
  43.             </list>   
  44.         </property>   
  45.     </bean>   
  46.     <bean id="client" class="com.tnt.mms.webservice.DatasetExcute"  
  47.         factory-bean="clientFactory" factory-method="create" />   
  48.   
  49.     <bean id="clientFactory"  
  50.         class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">   
  51.         <property name="serviceClass"  
  52.             value="com.tnt.mms.webservice.DatasetExcute" />   
  53.         <property name="address"  
  54.             value="http://localhost:8080/extjsmms/services/DatasetExcute" />   
  55.             <!--  -->   
  56.         <property name="serviceFactory"  
  57.             ref="jaxwsAndAegisServiceFactory" />   
  58.     </bean>   
  59.   
  60. </beans>   
  61. <!-- END SNIPPET: beans -->  
<?xml version="1.0" encoding="UTF-8"?>
<!--
	Licensed to the Apache Software Foundation (ASF) under one
	or more contributor license agreements. See the NOTICE file
	distributed with this work for additional information
	regarding copyright ownership. The ASF licenses this file
	to you under the Apache License, Version 2.0 (the
	"License"); you may not use this file except in compliance
	with the License. You may obtain a copy of the License at
	
	http://www.apache.org/licenses/LICENSE-2.0
	
	Unless required by applicable law or agreed to in writing,
	software distributed under the License is distributed on an
	"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
	KIND, either express or implied. See the License for the
	specific language governing permissions and limitations
	under the License.
-->
<!-- START SNIPPET: beans -->
<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-2.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">
	<!-- Configure CXF to use Aegis data binding instead of JAXB -->
	<bean id="aegisBean"
		class="org.apache.cxf.aegis.databinding.AegisDatabinding"
		scope="prototype" />
	<bean id="jaxwsAndAegisServiceFactory"
		class="org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean"
		scope="prototype">
		<property name="dataBinding" ref="aegisBean" />
		<property name="serviceConfigurations">
			<list>
				<bean
					class="org.apache.cxf.jaxws.support.JaxWsServiceConfiguration" />
				<bean
					class="org.apache.cxf.aegis.databinding.AegisServiceConfiguration" />
				<bean
					class="org.apache.cxf.service.factory.DefaultServiceConfiguration" />
			</list>
		</property>
	</bean>
	<bean id="client" class="com.tnt.mms.webservice.DatasetExcute"
		factory-bean="clientFactory" factory-method="create" />

	<bean id="clientFactory"
		class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
		<property name="serviceClass"
			value="com.tnt.mms.webservice.DatasetExcute" />
		<property name="address"
			value="http://localhost:8080/extjsmms/services/DatasetExcute" />
			<!--  -->
		<property name="serviceFactory"
			ref="jaxwsAndAegisServiceFactory" />
	</bean>

</beans>
<!-- END SNIPPET: beans -->


【总结】
1.当用到参数传递值时千万不要用acegi,不知道bug否,xfire的时候就发现有这个问题现在也未解决。
2.后续要认真研究aegis,没准能找到问题。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值