10
06月
2025
SOAP是一种简易对象访问的通信协议,主要基于XML格式,独立于语言,用于系统与系统直接信息交互。
JAVA 调用SOAP接口 Axis解决方案
废话不多说,直接看
1:POM.xml 引入
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis-jaxrpc</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis-saaj</artifactId>
<version>1.4</version>
</dependency>
2:SoapUtil.java 基类方法
import com.alibaba.fastjson.JSON;
import com.richjob.syncjob.entity.PageBaseResponse;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.axis.handlers.soap.SOAPService;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import java.util.List;
import java.util.Map;
public class SoapUtil {
private String endpointAddress;
private String serviceName;
private String namespace;
private String methodName;
private int timeOut;
private List<String> paramNames;
public SoapUtil(String endpointAddress, String serviceName, String namespace, String methodName, int timeOut, List<String> paramNames) {
this.endpointAddress = endpointAddress;
this.serviceName = serviceName;
this.namespace = namespace;
this.methodName = methodName;
this.timeOut = timeOut;
this.paramNames = paramNames;
}
public String doOpertion(Object[] paramer) throws Exception {
//soapAction
String soapAction = namespace+methodName;
Service service = new Service();
Call call = (Call) service.createCall();
//设置响应超时
call.setTimeout(timeOut);
//设置地址
call.setTargetEndpointAddress(endpointAddress);
//设置方法名
call.setOperationName(new QName(namespace,methodName));
for (String paramName:paramNames) {
call.addParameter(paramName, XMLType.XSD_STRING, ParameterMode.IN);
}
//设置返回类型
call.setReturnType(XMLType.XSD_STRING);
//启用soap
call.setUseSOAPAction(true);
//设置soapAction
call.setSOAPActionURI(soapAction);
//设置服务名
SOAPService soapService = new SOAPService();
soapService.setName(serviceName);
call.setSOAPService(soapService);
String ret = (String) call.invoke(paramer);
XmlUtil xmlUtil=new XmlUtil();
Map map= xmlUtil.xmlToMap(ret);
return ret;
}
public PageBaseResponse GetPageInfosOpertion(Object[] paramer) throws Exception {
//soapAction
String soapAction = namespace+methodName;
Service service = new Service();
Call call = (Call) service.createCall();
//设置响应超时
call.setTimeout(timeOut);
//设置地址
call.setTargetEndpointAddress(endpointAddress);
//设置方法名
call.setOperationName(new QName(namespace,methodName));
for (String paramName:paramNames) {
call.addParameter(paramName, XMLType.XSD_STRING, ParameterMode.IN);
}
//设置返回类型
call.setReturnType(XMLType.XSD_STRING);
//启用soap
call.setUseSOAPAction(true);
//设置soapAction
call.setSOAPActionURI(soapAction);
//设置服务名
SOAPService soapService = new SOAPService();
soapService.setName(serviceName);
call.setSOAPService(soapService);
String ret = (String) call.invoke(paramer);
PageBaseResponse pageBaseResponse= JSON.parseObject(ret,PageBaseResponse.class);
return pageBaseResponse;
}
}
3:调用方式
List<SyncHrmResource> syncHrmResources = new ArrayList<>();
BatchEditHrmResource(syncHrmResources, "add")
4:调用方式
/* 批量
* 同步人力资源信息
* 新增
* 编辑
* @param operType
* @return
*/
public <T> String BatchEditHrmResource(List sysncList,String operType) throws Exception {
List<String> paramNames=new ArrayList<>();
paramNames.add("in0");
paramNames.add("in1");
Object[] paramer=new Object[2];
paramer[0]="";
StringBuffer stringBuffer=new StringBuffer();
stringBuffer.append("<?xml version='1.0' encoding='UTF-8'?><root><hrmlist>");
for (Object obj:sysncList) {
stringBuffer.append("<hrm action='");
stringBuffer.append(operType);
stringBuffer.append("'>");
stringBuffer.append(XmlUtil.objToXml(obj));
stringBuffer.append("</hrm>");
}
stringBuffer.append("</hrmlist></root>");
XxlJobLogger.log("同步人力资源信息参数为--"+JSONObject.toJSONString(sysncList));
paramer[1]=stringBuffer.toString();
SoapUtil soapUtil=new SoapUtil(FinalStr.baseOaUrl,"SynHrmResource","","SynHrmResource",150000,paramNames);
String ret = soapUtil.doOpertion(paramer);
XxlJobLogger.log("同步人员信息接口返回:" +ret);
return ret;
}
PS:上面的最终stringBuffer字符形式是
<?xml version='1.0' encoding='UTF-8'?>
<root>
<hrmlist>
<hrm action='edit'>
<workcode>xy0036</workcode>
<subcompany>{JSON}{"subcompanycode":"md1jt"}</subcompany>
</hrm>
</hrmlist>
</root>
具体要根据接口文档规范要求来