WebService初学

来源:互联网 发布:成都办公软件培训 编辑:程序博客网 时间:2024/06/05 00:28

使用工具:MyEclipse8.5+tomcat6

1.      file-->new Web Service Project,填好项目名称,framework选择XFire,如图一

图一

2.      next-->next选择下面三项如图二

图二

3.选择finish,出现如下所示项目如图三

图三

4.src下新建packagewebservice),接口HelloWorld。其实现类HelloWorldImpl如图四

图四

5.impl代码

package webservice; publicclass HelloWorldImpl implements HelloWorld {     public String sayHelloWithParameter(String str) {       // TODO Auto-generated method stub       return"hello"+str;    }     publicvoid sayHelloWithoutParameter() {       // TODO Auto-generated method stub     } }


6.配置WebServiceservices.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://xfire.codehaus.org/config/1.0">    <service>       <name>Hello</name>       <namespace>http://localhost:8080/Hello/</namespace>       <serviceClass>webservice.HelloWorld</serviceClass>       <implementationClass>webservice.HelloWorldImpl</implementationClass>    </service></beans>


7.输入http://localhost:8080/Hello/services如图五

图五

8.点击wsdl,如图六

图六

9.test代码

package webservice; 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;  public class Test { /** * @param args */public static void main(String[] args) {        Service srvcModel = new ObjectServiceFactory().create(HelloWorld.class);          XFireProxyFactory factory = new XFireProxyFactory(XFireFactory.newInstance().getXFire());        System.out.println(factory);                String helloWorldURL = "http://localhost:8080/Hello/services/Hello";                  try {                             HelloWorld srvc = (HelloWorld) factory.create(srvcModel,helloWorldURL);                              String result = srvc.sayHelloWithParameter("rrr");                              System.out.println(result);                      } catch (MalformedURLException e) {                             // TODO Auto-generated catch block                             e.printStackTrace();                      }              } }


输出

org.codehaus.xfire.client.XFireProxyFactory@c24c0

hellorrr

 

原创粉丝点击