xfire实现webService

  最近有点时间来研究xfire实现webservice,看了看感觉还挺简单的,简单是简单,还是做一下笔记吧,以后可能会用到又忘记。
   所要的jar包,都在附件里面,就直接进入正题了
   1,定义接口
  
public interface MailWebService {
	
	public abstract boolean send(String username,String password,String mailto,String title,String context);
}
   

  2,实现接口
 public class MailWebServiceImpl implements MailWebService{

	public boolean send(String username, String password, String mailto,
			String title, String context) {
		
		boolean flag = false;
		try {
			flag = HttpClientMail.send163WebMail(username, password, mailto, title, context);
		} catch (Exception e) {
			flag = false;
		}
		
		return flag;
	}

}

3,新建xfire.xml文件,记得要将xfire.xml文件配置到web.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" />
    <!-- 使用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>	
	
	<!-- 
	  	建立web service服务 
	-->  
	<bean id="MailBean" class="com.yysoft.webservice.impl.MailWebServiceImpl"/>
    <bean id="Mail" parent="baseWebService">        
        <property name="serviceBean" ref="MailBean" />
        <property name="serviceClass" value="com.yysoft.webservice.MailWebService" /> 
    </bean>
    
</beans>

4,当然还要定义怎么样去访问我们的接口,在web.xml配置。
<!-- xfire -->
	<servlet>
	     <servlet-name>XFireServlet</servlet-name>
	     <servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</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> 

5,测试
public class MailClient {
	
	public static void main(String[] args) {
		try {
			String[] parameters = {"test","test", "[email protected]", "测试", "测试内容"};	
			
			Client client = new Client(new URL("http://localhost:8089/services/MailWebService?wsdl"));
			Object[] results = client.invoke("send", parameters);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}
}

你可能感兴趣的:(webservice)