JSP学习笔记(一百二十二):使用axis1.4开发webservice应用(一)
1.下载axis1.4
下载地址:http://axis.apache.org/axis/ 选择Axis 1.4 Final,下载axis-bin-1_4.zip即可。
2.准备工作
把下载的axis中的jar包放到web项目中,至少需要如下包:
axis.jar saaj.jar wsdl4j-1.5.1.jar jaxrpc.jar commons-discovery-0.2.jar commons-logging-1.0.4.jar
web.xml中添加如下代码:
<servlet> <servlet-name>AxisServlet</servlet-name> <servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping>
3.编写服务端代码
web项目发布目录下(我的是WebContent目录)创建文件夹services,services文件夹下创建文件SayHello.jws,内容如下:
public class SayHello { public String getName(String name) { return "hello," + name; } }
访问该服务是否正常 http://localhost:8080/proj1/services/SayHello.jws
访问后会自动创建对应的目录和class文件 WEB-INF/jwsClasses/services/SayHello.class
4.编写客户端代码
String endPoint = "http://localhost:8080/proj1/services/SayHello.jws"; Service service = new Service(); Call call = (Call)service.createCall(); call.setOperationName(new QName(endPoint,"getName")); call.setTargetEndpointAddress(new URL(endPoint)); String result = (String)call.invoke(new Object[]{"abcddd"}); System.out.println(result);
执行该代码打印内容为:hello,abcddd
5.客户端代码传输自定义对象
Employee employee = new Employee(); // ...... employee对象的描述 QName qn = new QName("urn:BeanService","EmployeeObject"); String endPoint = "http://localhost:8080/proj1/services/SayHello.jws"; Service service = new Service(); Call call = (Call)service.createCall(); call.setOperationName(new QName(endPoint,"getName")); call.setTargetEndpointAddress(new URL(endPoint)); call.registerTypeMapping(Employee.class, qn, new BeanSerializerFactory(Employee.class,qn), new BeanDeserializerFactory(Employee.class,qn)); String result = (String)call.invoke(new Object[]{employee}); System.out.println(result);