闲话不说,直接上。(对于xfire的介绍等的东西,见百度或谷歌)
1、下载xfire。地址: http://xfire.codehaus.org/Download
xfire-distribution-1.2.6.zip
xfire-all-1.2.6.jar
2、新建j2ee项目。导入jar包
3、在web.xml中配置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>
4、创建webservice服务接口和实现类
HelloWorld.java
public interface HelloWorld { public String sayHello(); }
HelloWorldImpl.java
public class HelloWorldImpl implements HelloWorld { @Override public String sayHello() { // TODO Auto-generated method stub System.out.println("Hello,all----------I'm in server."); return "Hello,all----------I'm in client."; } }
5、配置xfire的文件。需要在META-INF下新建文件夹xfire,在xfire文件夹下创建services.xml文件
services.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://xfire.codehaus.org/config/1.0"> <service> <name>HelloService</name> <namespace>http://localhost:8080/myxfire</namespace> <serviceClass>com.HelloWorld</serviceClass> <implementationClass>com.HelloWorldImpl</implementationClass> </service> </beans>
6、启动taomcat。使用浏览器打开http://localhost:8080/myxfire/services/
如果这一步报错:
class path resource [META-INF/xfire/services.xml] cannot be opened because it does not exist
则需要将META-INF文件夹拷到WEB-INF文件夹下,如下图:
7、调用webservice
public static void main(String[] args) { String url = "http://localhost:8080/myxfire/services/HelloService?wsdl"; try { Client client = new Client(new URL(url)); Object [] obj = client.invoke("sayHello",new Object[]{}); System.out.println(obj[0]); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }