最近学习Axis2这个webService。将一些基础但重要的知识简单总结一下。这里是内容是建立在Axis2已经部署在tomcat的情况下(部署过程很简单,在Apache网站上下载axis2的war包然后解压到tomcat/webapps下,此时访问http://localhost:8080/axis2/services/listServices便可知是否正常)
在阅读《Apache Axis2 Web Services 2nd Edition》这本书的时候,在关于如果发布一个WebServices书中提到了两种方法。
方法一:POJO approach
现在我们编写一个简单的POJO类,这个类不包含在任何包下:
public class HelloWorld {
public String sayHello(String name) {
return "Hello " + name;
}
}
接下来的操作就是把这个普通的POJO发布为webServices让其他人可以远程调用。
发布过程:
- Compile the Java class, which will produce the HelloWorld.class file.
- Go to /webapps/axis2/WEB-INF.
- Create a directory named pojo inside the WEB-INF directory.
- Then copy the HelloWorld.class into the pojo directory, created in the
previous step. - Start Tomcat.
- Go to http://localhost:8080/axis2/services/listServices , where
you will be able to find a service named HelloWorld listed
按照如上的步骤即可通过http://localhost:8080/axis2/services/listServices 这个URL可以看到我们刚刚发布的webServices。注意,这种方法只适合没有包的POJO方法。
通过浏览器访问该webServices:http://localhost:8080/axis2/services/HelloWorld/sayHello?name=Axis2
此时浏览器会打印:
<ns:sayHelloResponse>
<return>Hello Axis2</return>
</ns:sayHelloResponse>
如何对于一个存在包结构的POJO类又该如何发布了?
package book.sample
import javax.jws.WebService;
@WebService
public class AddressService {
@WebMethod
public Address getAddress(String name) {
Address address = new Address();
address.setStreet("Street");
address.setNumber("Number 15");
return address;
}
}
后面解释这两个注解的含义
package book.sample
public class Address {
private String street;
private String number;
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
现在除了这两个POJO还不够。我们还需要建立一个META-INF的文件夹(如果存在包的话这个文件夹和POJO类的包同级),并在这个文件夹下面创建services.xml
<service name="AddressService">
<description>
This is my first service, which says "Address!"
</description>
<parameter name="ServiceClass">AddressService</parameter>
<operation name="getAddress">
<messageReceiver class="org.apache.axis2.rpc.receivers.
RPCMessageReceiver"/>
</operation>
</service>
现在需要把两个POJO类的class文件,META-INF文件夹打包成aar文件(jar文件改名即可)然后将aar包放入%tomcat_home%\webapps\axis2\WEB-INF\services目录下。
现在解释上面使用的两个注解的含义:
A .jar file will, more often than not, contain more than one .class file in it. So there
should be a way to identify the service class (or classes) in it. That is why we have
annotated the POJO class with @WebService. Provided this annotation is applied,
Axis2 will identify that class as the service implementation class and expose it as a
web service.
大意为:一般打包jar文件时jar文件中一般存在多个class文件,需要通过标识符表示哪个才是对外发布的。
本文是关于Apache Axis2的入门介绍,详细阐述了如何在已部署的Tomcat上发布Web服务。首先,介绍了如何将一个简单的POJO转换为Web服务,包括编译Java类、复制到特定目录,并在Tomcat中查看服务是否成功发布。接着,讨论了针对带有包结构的POJO的发布方法,需要创建META-INF文件夹和服务.xml配置文件。最后,解释了@WebService注解的作用,用于标识服务实现类。
354

被折叠的 条评论
为什么被折叠?



