开始编写第一个webservice程序。
1、在web.xml文件中添加如下代码:
<servlet>
<servlet-name>XFireServlet</servlet-name>
<servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</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>
在web中定义了一个XFireServlet 的servlet,它负责提供webservice,并且提供每个web service的WSDL。如果你发布一个了web service 例如:HelloService,就可以通过网址 http://localhost:8080/hfa/services/ 来访问,结果如下:
Available Services:
•HelloService [wsdl]
Generated by XFire ( http://xfire.codehaus.org )
--------------------------------------------------------------------------------
访问HelloService的WSDL就用地址:http://localhost:8080/hfa/services/HelloService?wsdl 运行结果如下:
<?xml version="1.0" encoding="UTF-8" ?>
- <wsdl:definitions targetNamespace="http://localhost:8080/hfa/services/HelloService" xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding" xmlns:tns="http://localhost:8080/hfa/services/HelloService" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
- <wsdl:types>
+ <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://localhost:8080/hfa/services/HelloService">
- <xsd:element name="getClients">
<xsd:complexType />
</xsd:element>
- <xsd:complexType name="anyType2anyTypeMap">
- <xsd:sequence>
- <xsd:element maxOccurs="unbounded" minOccurs="0" name="entry">
- <xsd:complexType>
- <xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="0" name="key" type="xsd:anyType" />
<xsd:element maxOccurs="1" minOccurs="0" name="value" type="xsd:anyType" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
- <xsd:element name="getClientsResponse">
- <xsd:complexType>
- <xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="out" nillable="true" type="tns:anyType2anyTypeMap" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
- <xsd:element name="sayHello">
- <xsd:complexType>
- <xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="in0" nillable="true" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
- <xsd:element name="sayHelloResponse">
- <xsd:complexType>
- <xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="out" nillable="true" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</wsdl:types>
- <wsdl:message name="getClientsRequest">
<wsdl:part name="parameters" element="tns:getClients" />
</wsdl:message>
- <wsdl:message name="sayHelloResponse">
<wsdl:part name="parameters" element="tns:sayHelloResponse" />
</wsdl:message>
- <wsdl:message name="getClientsResponse">
<wsdl:part name="parameters" element="tns:getClientsResponse" />
</wsdl:message>
- <wsdl:message name="sayHelloRequest">
<wsdl:part name="parameters" element="tns:sayHello" />
</wsdl:message>
- <wsdl:portType name="HelloServicePortType">
- <wsdl:operation name="getClients">
<wsdl:input name="getClientsRequest" message="tns:getClientsRequest" />
<wsdl:output name="getClientsResponse" message="tns:getClientsResponse" />
</wsdl:operation>
- <wsdl:operation name="sayHello">
<wsdl:input name="sayHelloRequest" message="tns:sayHelloRequest" />
<wsdl:output name="sayHelloResponse" message="tns:sayHelloResponse" />
</wsdl:operation>
</wsdl:portType>
- <wsdl:binding name="HelloServiceHttpBinding" type="tns:HelloServicePortType">
<wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
- <wsdl:operation name="getClients">
<wsdlsoap:operation soapAction="" />
- <wsdl:input name="getClientsRequest">
<wsdlsoap:body use="literal" />
</wsdl:input>
- <wsdl:output name="getClientsResponse">
<wsdlsoap:body use="literal" />
</wsdl:output>
</wsdl:operation>
- <wsdl:operation name="sayHello">
<wsdlsoap:operation soapAction="" />
- <wsdl:input name="sayHelloRequest">
<wsdlsoap:body use="literal" />
</wsdl:input>
- <wsdl:output name="sayHelloResponse">
<wsdlsoap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
- <wsdl:service name="HelloService">
- <wsdl:port name="HelloServiceHttpPort" binding="tns:HelloServiceHttpBinding">
<wsdlsoap:address location="http://localhost:8080/hfa/services/HelloService" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
2、在项目src的包中新建接口HelloService 代码如下:
package com.zhongrui.pms.business.service.webservice;
import java.util.List;
import java.util.Map;
/**
*fileName: HelloService.java
*author:forrest li
*time: 2012-7-31 上午10:12:44
**/
public interface HelloService {
public String sayHello(String name);
public Map getClients();
}
实现类如下HelloServiceImp.java 代码如下:
package com.zhongrui.pms.business.service.webservice;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.zhongrun.frame.dao.JdbcTemplateDaoUtil;
/**
*fileName: HelloServiceImp.java
*author:forrest li
*time: 2012-7-31 上午10:14:35
**/
public class HelloServiceImp implements HelloService {
private static final Log logger = LogFactory.getLog(HelloServiceImp.class);
private static final JdbcTemplateDaoUtil dao = new JdbcTemplateDaoUtil();
public String sayHello(String name) {
// TODO Auto-generated method stub
logger.info("---name------"+name);
return "from service say hello!"+name;
}
public Map<String, Object> getClients() {
// TODO Auto-generated method stub
Map map = new HashMap();
map.put("ID", "351");
String sql = "SELECT * FROM CLIENT_INFO t where t.CLIENT_ID=:ID";
List<Map<String, Object>> list = dao.getAllEntity(sql, map);
logger.info(" list =================:"+list);
for(int i= 0;i<list.size();i++){
logger.info("map-----------------"+list.get(i));
}
return list.get(0);
}
}
3、在资源目录src/java/sources/src下新建文件夹如下图:
4、编辑services.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<service xmlns="http://xfire.codehaus.org/config/1.0">
<name>HelloService</name>
<namespace>http://localhost:8080/hfa/services/HelloService</namespace>
<serviceClass>com.zhongrui.pms.business.service.webservice.HelloService</serviceClass>
<implementationClass>com.zhongrui.pms.business.service.webservice.HelloServiceImp</implementationClass>
</service>
</beans>
5、启动tomcat服务,加载hfa工程。
6、输入地址:http://localhost:8080/hfa/services/ 或者 http://localhost:8080/hfa/services/HelloService?wsdl 。运行结果见 1、。