`
erichua
  • 浏览: 510303 次
  • 性别: Icon_minigender_2
  • 来自: 远方
社区版块
存档分类
最新评论

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

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

【Server】
1、Interface
@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文件
@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、描述文件
	<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】
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.配置文件
<?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
0
分享到:
评论
1 楼 futoviny 2017-04-28  
挺有用的   javax.xml.ws.Holder

相关推荐

Global site tag (gtag.js) - Google Analytics