|
开发了一个webservice服务端,采用的spring+Hibernate管理数据库。spring加载applicationContext.xml采用代码一。在服务端写了一个测试类(public static void main(String[] args))测试已经发布成webservice的方法A(注:方法A调用service层的方法B,方法B有连接数据库的操作,并且使用了事务管理(代码二)),同时,用 代码一
ApplicationContext ctx = new FileSystemXmlApplicationContext("file:E:/smartSaleWebservice/WebRoot/WEB-INF/applicationContext.xml");
BaseInfoService srvBaseInfo=(BaseInfoService)ctx.getBean("srvBaseInfo");
代码二 <!-- 事务处理的AOP配置 --> <bean id="ProxyTemplate" abstract="true" lazy-init="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"><ref local="myTransactionManager" /></property> <property name="transactionAttributes"> <props> <prop key="insert*">PROPAGATION_REQUIRED</prop> <prop key="search*">PROPAGATION_REQUIRED</prop> <prop key="query*">PROPAGATION_REQUIRED</prop> <prop key="one*">PROPAGATION_REQUIRED</prop> <prop key="isExit*">PROPAGATION_REQUIRED</prop> <prop key="send*">PROPAGATION_REQUIRED</prop> <prop key="get*">PROPAGATION_REQUIRED</prop> <prop key="execute*">PROPAGATION_REQUIRED</prop> <prop key="add*">PROPAGATION_REQUIRED</prop> <prop key="del*">PROPAGATION_REQUIRED</prop> <prop key="save*">PROPAGATION_REQUIRED</prop> <prop key="esave*">PROPAGATION_REQUIRED</prop> <prop key="E_*">PROPAGATION_REQUIRED</prop> <prop key="up*">PROPAGATION_REQUIRED</prop> <prop key="Eup*">PROPAGATION_REQUIRED</prop> <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="dis*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="part*">PROPAGATION_REQUIRED</prop> <prop key="eChange*">PROPAGATION_REQUIRED</prop> <prop key="eCacle*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> 代码三
发布成webservice的方法,即方法A
/**
*查询部门信息
* @param xmlStr
* @return
*/
public String findDepartmentNum(String depId){
List<smartSaleDepartment> Department=null;
System.out.println("------->1");
try{
Department=srvBaseInfo.findDepartment(depId);
}catch(Exception ex){
logger.error("findDepartmentNum error--->",ex);
}
ListNum=(Department==null?0:Department.size());
return String.valueOf(Department==null?0:Department.size());
}
代码四 serviceImpl的方法,即方法B
public List<smartSaleDepartment> findDepartment(String depId){
String selHql="from smartSaleDepartment where id="""+depId+"""";
System.out.println("------->2");
try{
List<smartSaleDepartment> DepartmentList=this.getDaoDepartment().executeSelectHql(selHql);
return DepartmentList;
}
catch(Exception ex){
logger.error("the findDepartment method in BaseInfoServiceImpl has got an exception:",ex);
return null;
}
}
代码五 客户端调用webservice的方法C
package com.smartSale;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import java.util.Calendar;
import java.util.Date;
import org.apache.axis.encoding.ser.BeanSerializerFactory;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.ParameterMode;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.log4j.Logger;
import org.apache.axis.encoding.XMLType;
import org.apache.axis.encoding.ser.BeanDeserializerFactory;
import org.apache.axis.encoding.ser.BeanSerializerFactory;
/**
* 和webservice的交互
* @author qianqingfu
*
*/
public class WStest {
static Logger logger = Logger.getLogger(WStest.class);// 用于记录日志
static String saleendpoint = "http://localhost:8080/smartSaleWS/services/SaleWebService";
public static String DepartmentNum(String depId){
Service service = new Service();
Call call = null;
String DepartmentNum = "";
try {
call = (Call) service.createCall();
call.setOperationName(new QName(baseendpoint,"findDepartmentNum"));
call.setTargetEndpointAddress(new java.net.URL(baseendpoint));
DepartmentNum = (String)call.invoke(new Object[]{depId});
}catch(Exception ex)
{
//ex.printStackTrace();
logger.error("-->webservice出现异常");
}
return DepartmentNum;
}
}
代码六 测试方法类
public class MainTest{
WStest wsTesting=new WStest();
public String departmentNum(String depId)
{
String depNum=wsTesting.DepartmentNum(depId);
return depNum;
}
public static void main(String[] args){
MainTest wsTest=new MainTest();
String depNum=wsTest.departmentNum("20090825150103963799");
if("0".equals(depNum))
{
System.out.print("这个门店并不存在,请核对相应信息");
}
else
{
System.out.print("这个门店存在!"+depNum);
}
}
}
|
|
|
selfup!
|
|
| 20分 |
呃,帮你顶下。。
|
| 30分 |
楼主你可以在web.xml中配置OpenSessionInViewFilter
|
|
The second selfup!
|
|