WebService + xml
Posted 斌灬小生不才
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WebService + xml相关的知识,希望对你有一定的参考价值。
一、请求参数
package com.zhicall.core.ws; import java.util.Map; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; public class WSRequest { private String methodName; private String wsurl; private String name; private Map<String,String> params; public WSRequest(){} public WSRequest(String wsurl,String name,Map<String,String> params,String methodName){ this.wsurl = wsurl; this.name = name; this.methodName = methodName; this.params = params; } public String getWsurl() { return wsurl; } public void setWsurl(String wsurl) { this.wsurl = wsurl; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Map<String, String> getParams() { return params; } public void setParams(Map<String, String> params) { this.params = params; } public String getMethodName() { return methodName; } public void setMethodName(String methodName) { this.methodName = methodName; } public String getRequestXml(){ Document doc = DocumentHelper.createDocument(); Element reqele = doc.addElement("Request"); reqele.addElement("FunName").setText(methodName); Element parele = reqele.addElement("Params"); if(params != null && !params.isEmpty()){ for (String key : params.keySet()) { Element param = parele.addElement("Param"); String value = params.get(key); param.addAttribute("Name", key).addAttribute("Value", value); } } return doc.asXML(); } }
二、结果获取
package com.zhicall.core.ws; import java.net.MalformedURLException; import java.rmi.RemoteException; import java.util.HashMap; import java.util.Map; import java.util.UUID; import javax.xml.namespace.QName; import javax.xml.rpc.ParameterMode; import javax.xml.rpc.ServiceException; import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.apache.axis.encoding.XMLType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class WebServiceUtil { private static Logger logger = LoggerFactory.getLogger(WebServiceUtil.class); public static String getWsResult(WSRequest request) { String result = "no result!"; try { String endpoint = request.getWsurl(); Service service = new Service(); Call call = (Call) service.createCall(); call.setTargetEndpointAddress(new java.net.URL(endpoint)); // 服务名 call.setOperationName(new QName("http://tempuri.org/", request.getName())); // 定义入口参数和参数类型 call.addParameter("Data", XMLType.XSD_STRING, ParameterMode.IN); call.setUseSOAPAction(true); // Action地址 // call.setSOAPActionURI("http://tempuri.org/"+"XHLisWebserviceFun"); // 定义返回值类型 call.setReturnType(new QName("http://www.w3.org/2001/XMLSchema-instance", "String")); // 调用服务获取返回值 String requestxml = request.getRequestXml(); logger.info("getWsResult:requestXML:"+requestxml); result = String.valueOf(call.invoke(new Object[] { requestxml })); logger.info("getWsResult:responseXML:"+result); return result.replace(" ", " "); } catch (ServiceException e) { logger.info("getWsResult:ERROR"+e.getMessage()); } catch (RemoteException e) { logger.info("getWsResult:ERROR"+e.getMessage()); } catch (MalformedURLException e) { logger.info("getWsResult:ERROR"+e.getMessage()); } return result; } }
三、XML解析成对象
XStream xs = new XStream(); xs.processAnnotations(new Class[]{ResponseResult.class,LisReponse.class}); Object obj = xs.fromXML(wsResult);
四、对象参数
package com.zhicall.domain.wsentity; import java.util.List; import org.apache.ibatis.type.Alias; import com.thoughtworks.xstream.annotations.XStreamAlias; @XStreamAlias("Response") public class ResponseResult<T> { @XStreamAlias("ResultCode") private String ResultCode; @XStreamAlias("ResultMsg") private String ResultMsg; @XStreamAlias("Items") private List<T> Items; public String getResultCode() { return ResultCode; } public void setResultCode(String resultCode) { ResultCode = resultCode; } public String getResultMsg() { return ResultMsg; } public void setResultMsg(String resultMsg) { ResultMsg = resultMsg; } public List<T> getItems() { return Items; } public void setItems(List<T> items) { Items = items; } }
以上是关于WebService + xml的主要内容,如果未能解决你的问题,请参考以下文章
从 XML 获取数据:NetSuite WebService
C# webservice 引用 dll 无法访问外部 xml 文件
在Tomcat的安装目录下conf目录下的server.xml文件中增加一个xml代码片段,该代码片段中每个属性的含义与用途
如何将 json 数据映射到 xml 以从 azure 函数 c# 调用 webservice