不带javaBean的SOAP调用
首先,下载soap.war,javamail.jar,activation.jar,soap.jar,xerces.jar,然后就是配置环境,如何配置就不细说了。
把soap.war扔到tomcat的webapps下,启动tomcat,访问http://127.0.0.1:8080/soap,出现Hello! Welcome to Apache-SOAP. 就说明环境搭建好了。
首先建立个java项目,包名叫soap,类名叫SOAPService,这个就是要远程调用的类
SOAPService.java:
# package soap;
#
# public class SOAPService
# {
# public Person sayHi(String name)
# {
# return "Hello! " + name;
# }
# }
然后打开soap的管理页面,http://127.0.0.1:8080/soap/admin
在管理页面点击“Deploy”按钮,配置部署这个类,填写基本信息如下:
ID urn:HelloWorld_SOAPService
Scope Application
Provider Type java
Provider Class soap.SOAPService
Use Static Class no
Methods sayHi
配置好后,编写soap的调用类SOAPClient
SOAPClient.java
# package com.kt.test;
#
# import java.net.MalformedURLException;
# import java.net.URL;
# import java.util.Vector;
#
# import org.apache.soap.Constants;
# import org.apache.soap.Fault;
# import org.apache.soap.SOAPException;
# import org.apache.soap.rpc.Call;
# import org.apache.soap.rpc.Parameter;
# import org.apache.soap.rpc.Response;
#
# public class SOAPClient
# {
# public static void main(String[] args) throws MalformedURLException, SOAPException
# {
# Call c = null;
# URL url = null;
# Vector params = null;
# Response rep = null;
#
# //传入的参数名
# String ourName = "lws";
#
# //部署soap时的唯一标示
# String ourUrn = "urn:HelloWorld_SOAPService";
#
# //远程调用的方法
# String ourMethod = "sayHi";
#
# //soap的url
# url = new URL("http://localhost:8080/soap/servlet/rpcrouter");
#
# System.out.println("Passing to our deployed " + ourUrn + " our name ("
# + ourName + "): ");
#
# c = new Call();
# c.setTargetObjectURI(ourUrn);
# c.setMethodName(ourMethod);
# c.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
#
# //设置参数
# params = new Vector();
# params.addElement(new Parameter("name", String.class, ourName, null));
#
# c.setParams(params);
#
# System.out.print("and its answer is: ");
#
# //Call的invoke返回org.apache.soap.rpc.Response
# rep = c.invoke(url, "");
#
# //rep.generatedFault()判断请求是否成功,不成功打印错误代码,请求成功就打印远程调用返回的值
# if (rep.generatedFault())
# {
# Fault fault = rep.getFault();
# System.out.println("\nCall failed!");
# System.out.println("Code = " + fault.getFaultCode());
# System.out.println("String = " + fault.getFaultString());
# } else
# {
# Parameter result = rep.getReturnValue();
# System.out.print(result.getValue() + " === ");
# System.out.println();
# }
# }
# }
此时把SOAPService.class拷贝到tomcat主目录的webapps下的soap/WEB-INF/classes下,注意包名,重启tomcat,运行SOAPClient.java文件,就应该可以看到打印出的远程返回的字符串“Hello! lws”。
带javaBean的SOAP调用
其本上大致相同,只是在远程调用类里要手动出则一下javaBean,部署soap的时候也需要添加用户自己的javaBean
SOAPService.java
1. package soap;
2.
3. public class SOAPService
4. {
5. public Person sayHi(Person person)
6. {
7. person.setName("name_1");
8. return person;
9. }
10. }
在编写Person类,在相同的包(soap)下
# package soap;
#
# public class Person
# {
# private String name;
#
# public String getName()
# {
# return name;
# }
#
# public void setName(String name)
# {
# this.name = name;
# }
# }
打开http://127.0.0.1:8080/soap/admin配置该soap类,信息如下:
ID urn:HelloWorld_SOAPService
Scope Application
Provider Type java
Provider Class soap.SOAPService
Use Static Class no
Methods sayHi
这是还需要配置javaBean,在Number of Mappings输入框输入1,它表示我们将给出一个映射(即Name JavaBean)的信息。紧接Mappings之下有一个表格,我们要用到这个表格的第一行。保留Encoding Style的值为SOAP,把NameSpace URI设置成对象的ID:在本例中,它是urn:Person。接下来,把Local Part和Java Type输入框设置成Name JavaBean的完整名字,即soap.Person。最后,把Java to XML Serializer和XML to Java Deserializer输入框设置成org.apache.soap.encoding.soapenc.BeanSerializer,这是一个实现了Serializer和Deserializer接口的类,用来串行化和反串行化Name JavaBean。如果你用到了更多的JavaBean(比如还有一个Address Bean),则应该在这个表格中输入其他Bean的信息,同时还应该更新Number of Mappings输入框的值,使之反映出表格中实际被使用的行数。
在SOAP调用程序中需要手工注册javaBean,完整代码如下;
SOAPClient.java
# package soap;
#
# import java.net.URL;
# import java.util.Vector;
#
# import org.apache.soap.Constants;
# import org.apache.soap.Fault;
# import org.apache.soap.encoding.SOAPMappingRegistry;
# import org.apache.soap.encoding.soapenc.BeanSerializer;
# import org.apache.soap.rpc.Call;
# import org.apache.soap.rpc.Parameter;
# import org.apache.soap.rpc.Response;
# import org.apache.soap.util.xml.QName;
#
# public class SOAPClient
# {
# public static void main(String[] arg) throws Exception
# {
# Call c = null;
# URL url = null;
# Vector params = null;
# Response rep = null;
# String ourName = "lws";
# String ourUrn = "urn:HelloWorld_SOAPService";
# String ourMethod = "sayHi";
# url = new URL("http://localhost:8080/soap/servlet/rpcrouter");
#
# System.out.println("Passing to our deployed " + ourUrn + " our name ("
# + ourName + "): ");
#
# c = new Call();
# c.setTargetObjectURI(ourUrn);
# c.setMethodName(ourMethod);
# c.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
#
# // 创建类型映射注册器
# SOAPMappingRegistry smr = new SOAPMappingRegistry();
# BeanSerializer beanSer = new BeanSerializer();
# // 映射类型
# smr.mapTypes(Constants.NS_URI_SOAP_ENC,
# new QName("urn:Person", "soap.Person"),soap.Person.class, beanSer, beanSer);
#
# c.setSOAPMappingRegistry(smr);
#
# params = new Vector();
# params.addElement(new Parameter("x", Person.class, new Person(),
# null));
# c.setParams(params);
#
# System.out.print("and its answer is: ");
#
# rep = c.invoke(url, "");
# if (rep.generatedFault())
# {
# Fault fault = rep.getFault();
# System.out.println("\nCall failed!");
# System.out.println("Code = " + fault.getFaultCode());
# System.out.println("String = " + fault.getFaultString());
# } else
# {
# Parameter result = rep.getReturnValue();
# Person entity = (Person)result.getValue();
# System.out.print( entity.getName()+ " === ");
# }
# }
# }
运行该程序,会打印出person对象中name的值"name_1"
提醒:如果要运行在MyEcplise建立的web项目上,会出错。
因为用 MyEcplise创建web项目,会自动加入javaee.jar,这里面已经封装了javamail和activation,所以造成了冲突,只要用解压工具打开javaee.jar文件,把里面的mail和activation删了就可以正常的访问了。