利用MyEclipse快速创建webService服务

创建web项目


利用MyEclipse快速创建webService服务_第1张图片
 

添加webservice支持,使用xfire


利用MyEclipse快速创建webService服务_第2张图片
 添加支持以后,MyEclipse自动配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 <!-- start 添加webservice支持以后,Myeclipse自动配置XFire -->
  <servlet>
    <servlet-name>XFireServlet</servlet-name>
    <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>
    <load-on-startup>0</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>XFireServlet</servlet-name>
    <url-pattern>/services/*</url-pattern>
  </servlet-mapping>
  <!-- end 添加webservice支持以后,Myeclipse自动配置XFire -->
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 创建接口

package com.hx.webservice;

public interface IWebServiceTest {
	public String getStr();
}

 创建实现类

package com.hx.webservice.imp;

import com.hx.webservice.IWebServiceTest;

public class WebServiceTest implements IWebServiceTest{

	public String getStr() {
		// TODO Auto-generated method stub
		return "webservice  调用成功!";
	}

}

 

配置service文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<service>  
        <!-- webserviceq名称,调用时需要指定这个 -->  
        <name>testService</name>  
        <!-- 自己服务的地址 -->  
        <namespace>http://localhost:8080/WebService</namespace>  
        <!-- 接口类 -->  
        <serviceClass>com.hx.webservice.IWebServiceTest</serviceClass>  
        <!-- 实现类 -->  
        <implementationClass>com.hx.webservice.imp.WebServiceTest</implementationClass>  
    </service>  
</beans>

 启动Tomcat,地址栏输入:http://localhost:8080/WebService/services/testService?wsdl


利用MyEclipse快速创建webService服务_第3张图片
 部署成功。

创建client端,访问webservice服务:

package com.hx.client;

import java.net.MalformedURLException;

import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;

import com.hx.webservice.IWebServiceTest;


public class Client {
	public static void main(String agrs[]){
		//这里是创建一个service,需要传入一个接口类,因为我们后面必须调用相应的接口方法
		Service srcModel = new ObjectServiceFactory().create(IWebServiceTest.class);
		//代理工厂,这里是为了后面创建相应的接口类
		XFireProxyFactory factory = new XFireProxyFactory(XFireFactory.newInstance().getXFire());
		//webservice地址,不需要加wsdl
		String testServiceUrl = "http://localhost:8080/WebService/services/testService";
		
		//利用工厂返回相应的接口类
		IWebServiceTest iTest = null;
		try {
			iTest = (IWebServiceTest)factory.create(srcModel,testServiceUrl);
			String str = iTest.getStr();
			System.out.println(str);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

 出现错误:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/httpclient/methods/RequestEntity
	at java.lang.Class.getDeclaredConstructors0(Native Method)
	at java.lang.Class.privateGetDeclaredConstructors(Class.java:2404)
	at java.lang.Class.getConstructor0(Class.java:2714)
	at java.lang.Class.getConstructor(Class.java:1674)
	at org.codehaus.xfire.transport.http.HttpChannel.sendViaClient(HttpChannel.java:108)
	at org.codehaus.xfire.transport.http.HttpChannel.send(HttpChannel.java:48)
	at org.codehaus.xfire.handler.OutMessageSender.invoke(OutMessageSender.java:26)
	at org.codehaus.xfire.handler.HandlerPipeline.invoke(HandlerPipeline.java:131)
	at org.codehaus.xfire.client.Invocation.invoke(Invocation.java:79)
	at org.codehaus.xfire.client.Invocation.invoke(Invocation.java:114)
	at org.codehaus.xfire.client.Client.invoke(Client.java:336)
	at org.codehaus.xfire.client.XFireProxy.handleRequest(XFireProxy.java:77)
	at org.codehaus.xfire.client.XFireProxy.invoke(XFireProxy.java:57)
	at $Proxy0.getStr(Unknown Source)
	at com.hx.client.Client.main(Client.java:26)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.httpclient.methods.RequestEntity
	at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
	... 15 more

 添加包commons-httpclient-3.0.jar,继续运行:



 成功,几分钟搞定。说到底任何技术都是方便我们使用。更多的精力还是要花在业务上面。

你可能感兴趣的:(webservice)