写在前面:最近工作中用到、具体原理没写、只有使用过程、记录一下、方便以后使用的时候回来看
1、先用soapUi访问webService接口地址、然后 在Java代码封装、请求的参数
先看一下需要封装的结构体、下面代码片从soapUI粘贴出来的
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://service.outsource.bussiness.server.com/">
<soapenv:Header/>
<soapenv:Body>
<ser:entranceOrLeaveOutSourcePersonInfo>
<!--Optional:-->
<msgHeader>
<!--Optional:-->
<comment>?</comment>
<!--Optional:-->
<count>?</count>
<!--Optional:-->
<interfaceID>?</interfaceID>
<!--Optional:-->
<messageID>?</messageID>
<!--Optional:-->
<receiver>?</receiver>
<!--Optional:-->
<sender>?</sender>
<!--Optional:-->
<transID>?</transID>
</msgHeader>
<!--Zero or more repetitions:-->
<persons>
<!--Optional:-->
<accessControl>?</accessControl>
<!--Optional:-->
<companyID>?</companyID>
<!--Optional:-->
<deptLevel>?</deptLevel>
<!--Optional:-->
<emailAddress>?</emailAddress>
<!--Optional:-->
<familyAddress>?</familyAddress>
<!--Optional:-->
<gender>?</gender>
<!--Optional:-->
<id>?</id>
<!--Optional:-->
<idCardNo>?</idCardNo>
<!--Optional:-->
<isApartment>?</isApartment>
<!--Optional:-->
<isParty>?</isParty>
<!--Optional:-->
<mobileNumber>?</mobileNumber>
<!--Optional:-->
<name>?</name>
<!--Optional:-->
<natureOfBusiness>?</natureOfBusiness>
<!--Optional:-->
<officeArea>?</officeArea>
<!--Optional:-->
<passportNo>?</passportNo>
<!--Optional:-->
<post>?</post>
<!--Optional:-->
<projectID>?</projectID>
<!--Optional:-->
<remark>?</remark>
<!--Optional:-->
<rowidObject>?</rowidObject>
<!--Optional:-->
<servicePosition>?</servicePosition>
<!--Optional:-->
<serviceType>?</serviceType>
<!--Optional:-->
<systemID>?</systemID>
<!--Optional:-->
<workPlaceCity>?</workPlaceCity>
<!--Optional:-->
<workPlaceProvince>?</workPlaceProvince>
</persons>
<!--Optional:-->
<operateType>?</operateType>
<!--Optional:-->
<operateId>?</operateId>
<!--Optional:-->
<operateUserId>?</operateUserId>
<!--Optional:-->
<operateUserName>?</operateUserName>
<!--Optional:-->
<operateUserDept>?</operateUserDept>
</ser:entranceOrLeaveOutSourcePersonInfo>
</soapenv:Body>
</soapenv:Envelope>
开始根据soapUI的xml 封装参数
//dto就是你实际业务场景需要用的数据
//拼接内容和soapUI访问的webService接口的的xml保持一致
private StringBuilder MDMPersonXml(xxxDTO dto) {
StringBuilder sbf = new StringBuilder();
sbf.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://service.outsource.bussiness.server.com/\">")
.append("<soapenv:Header/>")
.append("<soapenv:Body>")
.append("<ser:entranceOrLeaveOutSourcePersonInfo>")
.append("<msgHeader>")
.append("<messageID>").append(new Date().getTime()).append("</messageID>")
.append("<interfaceID></interfaceID>")
.append("<transID>").append(new Date().getTime()).append("</transID>")
.append("<sender>JF_OPM</sender>")
.append("<receiver>JF_MDM</receiver>")
.append("<count></count>")
.append("<comment></comment>")
.append("</msgHeader>");
for (TpmPersonnelDTO personnel : dto.getPersonnelList()) {
sbf.append("<persons>")
.append("<accessControl>").append(getMapval(personnel.getAccessControl())).append("</accessControl>")
.append("<deptLevel>").append(getMapval(personnel.getDeptLevel())).append("</deptLevel>")
.append("<emailAddress>").append(getMapval(personnel.getEmailAddress())).append("</emailAddress>")
.append("<familyAddress>").append(getMapval(personnel.getFamilyAddress())).append("</familyAddress>")
.append("<gender>").append(getMapval(personnel.getGender())).append("</gender>")
.append("<id>").append(getMapval(personnel.getMdmApplyId())).append("</id>")
.append("<idCardNo>").append(getMapval(personnel.getIdCardNo())).append("</idCardNo>")
.append("<isApartment>").append(getMapval(personnel.getIsApartment())).append("</isApartment>")
.append("<isParty>").append(getMapval(personnel.getIsParty())).append("</isParty>")
.append("<mobileNumber>").append(getMapval(personnel.getMobileNumber())).append("</mobileNumber>")
.append("<name>").append(getMapval(personnel.getName())).append("</name>")
.append("<natureOfBusiness>").append(getMapval(personnel.getNatureOfBusiness())).append("</natureOfBusiness>")
.append("<officeArea>").append(getMapval(personnel.getOfficeArea())).append("</officeArea>")
.append("<passportNo>").append(getMapval(personnel.getPassportNo())).append("</passportNo>")
.append("<post>").append(getMapval(personnel.getPost())).append("</post>")
.append("<remark>").append(getMapval(personnel.getRemark())).append("</remark>")
.append("<rowidObject>").append(getMapval(personnel.getRowidObject())).append("</rowidObject>")
.append("<servicePosition>").append(getMapval(personnel.getServicePosition())).append("</servicePosition>")
.append("<workPlaceCity>").append(getMapval(personnel.getWorkPlaceCity())).append("</workPlaceCity>")
.append("<workPlaceProvince>").append(getMapval(personnel.getWorkPlaceProvince())).append("</workPlaceProvince>")
.append("<projectID>").append(getMapval(personnel.getProjectId())).append("</projectID>")
.append("<systemID>").append(getMapval(personnel.getSystemId())).append("</systemID>");
append("<serviceType>").append(getMapval(dto.getPersonnelType())).append("</serviceType>");
.append("<companyID>").append(getMapval(personnel.getCompanyId())).append("</companyID>")
.append("</persons>");
}
SysUserUtilDTO dept = sysUserService.getDept(dto.getCreator());
sbf.append("<operateType>").append(getMapval(dto.getType())).append("</operateType>")
.append("<operateId>").append(dto.getId()).append("</operateId>")
.append("<operateUserId>").append(getMapval(dto.getApplicant())).append("</operateUserId>")
.append("<operateUserName>").append(getMapval(dto.getApplicantName())).append("</operateUserName>")
.append("<operateUserDept>").append(getMapval(dto.getCompany()))
.append(',' + getMapval(dept.getDeptIdTwo()))
.append(',' + getMapval(dept.getDeptIdOne()))
.append("</operateUserDept>")
.append("</ser:entranceOrLeaveOutSourcePersonInfo>")
.append("</soapenv:Body>")
.append("</soapenv:Envelope>");
return sbf;
}
根据封装好的请求参数、请求webService接口
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* 发起WebService请求
*
* @param url 请求url
* @param request 请求
* @return 请求结果
*/
private String getWebServiceData2(String url, StringBuilder request) {
System.out.println("发起WebService请求");
System.out.println("url");
StringBuilder result = new StringBuilder();
try {
URL restUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) restUrl.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/xml; charset=utf-8");
conn.setDoOutput(true);
PrintStream ps = new PrintStream(conn.getOutputStream());
ps.print(request);
ps.close();
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = "";
while ((line = br.readLine()) != null) {
result.append(line);
}
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
logger.error("异常信息=="+e.toString());
}
return result.toString();
}