根据上一篇博文安装好了service和codegen以后,新建一个项目,随便写一个方法,
一个简单的webservice项目
我写的代码非常简单:

package webservicetest1;

public class HelloWorld {
	public String sayHello() {
		return("hello");
	}
}

然后使用Axis service Archiver把它变成webservice.首先class file location这里填tomcat里该项目的位置,到classes为止,例如我的是D:\apache-tomcat-7.0.90\webapps\webservicetest1\WEB-INF\classes
一个简单的webservice项目
第二步,skip wsdl
一个简单的webservice项目
->next->next->next,直到这里
一个简单的webservice项目
service name随便写,我的是HelloWorld,class name是包名和类名(记住中间用.隔开,而不是/),然后点击load,next。
这时候在浏览器输入http://localhost:8080/axis2/services/HelloWorld?wsdl已经可以看到wsdl文件如图:
一个简单的webservice项目
也可以输入http://localhost:8080/axis2/,点击services,看到我们的服务HelloWorld,其中有个可用的方法sayHello,
一个简单的webservice项目
还可以输入http://localhost:8080/axis2/services/HelloWorld,这时显示的就是我们服务的返回值了。
一个简单的webservice项目
然后建立一个webservice的client,我试过cmd用代码生成client代码,但结果是少了porttype类,也试过new->other->webservice client,结果也是生成了几个类全部报错。好像是少生成了类吧,具体原因我没去细究了
一个简单的webservice项目
直接换了一种方法,用这个一个简单的webservice项目
生成代码如图:
一个简单的webservice项目
刚开始还是全部报错的,后来把Axis的jar包导进去就基本没错了(除了两个地方少了参数,用eclipse提示的方法把参数加上就完事了),需要导的jar包如下(网上说是wenservice所需要的最小jar包)

<dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2</artifactId>
            <version>1.6.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-adb</artifactId>
            <version>1.6.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-transport-http</artifactId>
            <version>1.6.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-transport-local</artifactId>
            <version>1.6.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-kernel</artifactId>
            <version>1.6.2</version>
        </dependency>

结束以后在client项目的src/main/test里写一个测试类,

import java.rmi.RemoteException;

import org.apache.axis2.AxisFault;

import webservicetest1.HelloWorldStub;
import webservicetest1.SayHello;

public class Test {
	public static void main(String[] args) throws RemoteException {
		HelloWorldStub helloWorldStub = new HelloWorldStub();
		SayHello sayHello = new SayHello();
		String aa = helloWorldStub.sayHello(sayHello).get_return();
		System.out.println(aa);
	}	
}

运行测试类就得到了服务所返回的结果:
一个简单的webservice项目

相关文章:

  • 2021-06-04
  • 2021-11-24
  • 2021-11-30
  • 2021-12-12
  • 2021-11-28
  • 2021-12-03
  • 2021-08-08
  • 2021-12-18
猜你喜欢
  • 2021-10-16
  • 2021-09-24
  • 2021-10-02
  • 2021-12-01
  • 2021-11-21
  • 2021-08-08
  • 2022-01-02
相关资源
相似解决方案