调用webservice接口参数可以传入实体类吗

2024年11月19日 10:37
有1个网友回答
网友(1):

java调用.net写的webservice 传入实体类作为参数

///


/// 添加windows用户
///

/// 单元名称
/// 用户对象
/// 所属组
/// 连接对象

public static string AddUser(string ldapDN, UsersInfo user, string group, string path, string name, string pwd)
{

DirectoryEntry de = GetFirectoryObject(path, name, pwd); //连接
DirectoryEntry subEntry = de.Children.Find("CN=" + ldapDN); //查找User单元
DirectoryEntry NewUser = subEntry.Children.Add("cn=" + user.UserPrincipalName, "user");
SetProperty(NewUser, "Description", user.Description);//描述
SetProperty(NewUser, "sn", user.SN); //姓
SetProperty(NewUser, "employeeID", user.EmployeeID);
SetProperty(NewUser, "givenname", user.GivenName); //名
SetProperty(NewUser, "initials", user.Initials); //英文缩写
SetProperty(NewUser, "displayName", user.displayName); //显示名称
SetProperty(NewUser, "physicalDeliveryOfficeName", user.PhysicalDeliveryOfficeName); //办公室地址
SetProperty(NewUser, "telephoneNumber", user.telephoneNumber); //办公室电话
SetProperty(NewUser, "otherTelephone", user.otherTelephone); //其他电话
SetProperty(NewUser, "C", user.C); //国家/地区
SetProperty(NewUser, "St", user.St); //省/自治区
SetProperty(NewUser, "L", user.L); //市/县
SetProperty(NewUser, "streetAddress", user.StreetAddress); //街道
SetProperty(NewUser, "postOfficeBox", user.PostOfficeBox); //邮政信箱
SetProperty(NewUser, "postalCode", user.PostalCode); //邮政编码
SetProperty(NewUser, "userPrincipalName", user.UserPrincipalName); //用户登录名
// SetProperty(NewUser, "logonHours",DateTime.Now.ToString()); //登录时间
SetProperty(NewUser, "accountExpires", user.AccountExpires); //账户过期
SetProperty(NewUser, "mail", user.Mail); //寻呼机
SetProperty(NewUser, "Pager", user.Pager); //寻呼机
SetProperty(NewUser, "mobile", user.Mobile); //移动电话
SetProperty(NewUser, "FacsimileTelephoneNumber", user.FacsimileTelephoneNumber); //传真
SetProperty(NewUser, "ipPhone", user.IpPhone); //IP电话
SetProperty(NewUser, "wWWHomePage", user.WWWHomePage); //网页
SetProperty(NewUser, "Info", user.Info); //注释
SetProperty(NewUser, "Title", user.Title); //职务
SetProperty(NewUser, "Department", user.Department); //部门
SetProperty(NewUser, "Company", user.Company); //公司
SetProperty(NewUser, "sAMAccountName", user.sAMAccountName); //登录名称曾用名
NewUser.CommitChanges();
NewUser.Properties["userPassword"].Add(user.UserPassWord);
NewUser.CommitChanges();
NewUser.Invoke("SetPassword", new object[] { user.UserPassWord });
NewUser.CommitChanges();
AddGroupUser(group, user.UserPrincipalName, de); //将用户添加到组
EnableAccount(NewUser);
return "已将用户" + ldapDN + "添加到组";
}
以下是服务端xml
POST /OperationAD.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length SOAPAction: "http://tempuri.org/AddUser"





string

string
string
string
string
string
string
string
string
string
string
string
string
string
string
string
string
string
string
string
string
string
string
string
string
string
string
string
string
string

string
string
string
string




java 客户端测试代码
package webservice;
import org.apache.axis.client.Service;
import org.apache.axis.client.Call;
import org.apache.axis.encoding.ser.BeanDeserializerFactory;
import org.apache.axis.encoding.ser.BeanSerializerFactory;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
public class TestAxisClient {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {

// webserviceURL
String endpoint = "http://localhost:20576/OperationAD.asmx?wsdl";
Service service = new Service();

Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endpoint));
/**
QName qn = new QName("user");
call.registerTypeMapping(UsersInfo.class, qn,
new BeanSerializerFactory(UsersInfo.class, qn)
, new BeanDeserializerFactory(UsersInfo.class, qn));
注册 bean
UsersInfo user = new UsersInfo();
user.C="sda";
user.Company="xiaosdfna";
user.Info="sdfajsodfji";
user.setInfo("sdafsdf");
*/
// 设置要调用的方法
call.setOperationName(new QName("http://tempuri.org/","AddUser"));
//该方法需要的参数
call.addParameter(new QName("http://tempuri.org/","ldapDN"),
org.apache.axis.Constants.XSD_STRING,
ParameterMode.IN);

//此处缺少一个实体类参数[java中有个UsersInfo.class实体类]
/**call.addParameter(new QName("http://tempuri.org/","user"),
org.apache.axis.Constants.XSD_STRING,UsersInfo.class,
ParameterMode.IN);
call.registerTypeMapping(UsersInfo.class,qx,new BeanSerializerFactory(UsersInfo.class, qx),new BeanDeserializerFactory(UsersInfo.class, qx));
call.addParameter(new QName("http://tempuri.org/","group"),
org.apache.axis.Constants.XSD_STRING,
ParameterMode.IN);*/
call.addParameter(new QName("http://tempuri.org/","path"),
org.apache.axis.Constants.XSD_STRING,
ParameterMode.IN);
call.addParameter(new QName("http://tempuri.org/","name"),
org.apache.axis.Constants.XSD_STRING,
ParameterMode.IN);
call.addParameter(new QName("http://tempuri.org/","pwd"),
org.apache.axis.Constants.XSD_STRING,
ParameterMode.IN);

call.setReturnClass(UsersInfo.class);
// 方法的返回值类型
call.setReturnType(org.apache.axis.Constants.XSD_STRING);
call.setUseSOAPAction(true);
call.setSOAPActionURI("http://tempuri.org/AddUser");
// 调用该方法,
String ret = (String)call.invoke(new Object[] { "22",user,"23","22","12","23"});
System.out.println("Successful = " + ret);

} catch (Exception e) {
System.err.println(e.toString());
}

}
}