今天终于把webservice搞完了,写个总结,我是在原来的项目中,进行webservice扩展的。
首先在已完成的项目中加入webservice支持,如图

下一步

接下来选择xfire包

finish 完成。
这样在你的项目中会产生xfire的工具包,这里完全可以自己将所需要的包放入lib下。
由于我的原始项目是ssh的,这里就不再说了,接下来进行xfire配置。
首先web.xml
<!-- begin XFire 配置 -->
<servlet>
<servlet-name>xfire</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>xfire</servlet-name>
<url-pattern>*.ws</url-pattern>
</servlet-mapping>
<!-- 配合Spring容器中XFire一起工作的Servlet-->
<servlet>
<servlet-name>XFireServlet</servlet-name>
<servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>XFireServlet</servlet-name>
<url-pattern>/servlet/XFireServlet/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>XFireServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>接下来在src下面建立xfire-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 引入XFire预配置信息 -->
<import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />
<!-- 定义访问的url -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
<entry key="/webservices.ws">
<ref bean="webservices" />
</entry>
</map>
</property>
</bean>
<!-- 使用XFire导出器 -->
<bean id="baseWebService" class="org.codehaus.xfire.spring.remoting.XFireExporter" lazy-init="false" abstract="true">
<!-- 引用xfire.xml中定义的工厂 -->
<property name="serviceFactory" ref="xfire.serviceFactory" />
<!-- 引用xfire.xml中的xfire实例 -->
<property name="xfire" ref="xfire" />
</bean>
<bean id="webservices" parent="baseWebService">
<!-- 业务服务bean -->
<property name="serviceBean" ref="webservicesService" />
<!-- 业务服务bean的窄接口类 -->
<property name="serviceClass" value="com.seavision.huayi2.service.IWebservice" />
</bean>
</beans>
这里说说为什么定义窄接口,原因xfire会导出spring整个接口,不能控制那些暴露给用户,这样做就不会将所有接口暴露给用户。
下来说说,在项目下生成的services.xml,这个里面是按照spring2.0的命名空间配置的,所以改写这个xml
<?xml version="1.0" encoding="UTF-8"?> <beans> </beans>
这样就不会出错了。
这样启动Tomcat,如果启动时抛以下异常,则删掉发布后lib中的spring1.2版本,原因与spring2冲突
org.xml.sax.SAXParseException: Document root element "beans", must match DOCTYPE root "null"还有一些就不多说了。
服务器启动后,在项目名后面键入/services 则出现wsdl,另存为即可。
看看我的spring配置文件 ,和上面的xfire-servlet.xml对应起来。
<!-- webservice -->
<bean id="iwebserviceTarget" class="com.seavision.huayi2.service.impl.WebserviceImpl">
<property name="businsesslogDAO">
<ref local="TBusinsessLogDAO"/>
</property>
<property name="sationmonthreportDAO">
<ref local="TStationMonthReportDAO"/>
</property>
</bean>
<bean id="webservicesService" parent="txProxyTemplate">
<property name="target" ref="iwebserviceTarget">
</property>
</bean>
service即实现类如下,此代码仅为本人项目代码,仅供参考,切勿抄送
public interface IWebservice {
/***************
* 取得基础数据表中所有数据,以基础数据建立日期的倒叙排序(HY0703)
* @throws DAOException
* ******************/
public List<TBusinsessLog> getYeWuList();
/*******************************
*增加工作站月工作计划信息
********************************/
public boolean insertYueJiHua(TStationMonthReport tStationMonthReport);
}
实现类
public class WebserviceImpl implements IWebservice{
TBusinsessLogDAO businsesslogDAO;
TStationMonthReportDAO sationmonthreportDAO;
public TStationMonthReportDAO getSationmonthreportDAO() {
return sationmonthreportDAO;
}
public void setSationmonthreportDAO(TStationMonthReportDAO sationmonthreportDAO) {
this.sationmonthreportDAO = sationmonthreportDAO;
}
public TBusinsessLogDAO getBusinsesslogDAO() {
return businsesslogDAO;
}
public void setBusinsesslogDAO(TBusinsessLogDAO businsesslogDAO) {
this.businsesslogDAO = businsesslogDAO;
}
public List<TBusinsessLog> getYeWuList(){
System.out.println("调用ok");
String hql="from TBusinsessLog as t order by t.cretateDate desc";
List<TBusinsessLog> list= new ArrayList<TBusinsessLog>();
try {
List blist=businsesslogDAO.find(hql);
for(int i=0;i<blist.size();i++){
list.add((TBusinsessLog) blist.get(i));
}
} catch (DAOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
/*******************************
*增加工作站月工作计划信息
********************************/
public boolean insertYueJiHua(TStationMonthReport tStationMonthReport) {
System.out.println("调用ok");
try {
System.out.println("++++++++++"+tStationMonthReport.getStationMonthReportId());
sationmonthreportDAO.save(tStationMonthReport);
return true;
} catch (DAOException e) {
e.printStackTrace();
return false;
}
}dao层就不说了,到此完毕。