码迷,mamicode.com
首页 > 编程语言 > 详细

java 调用webservice并解析xml结果

时间:2020-06-18 15:51:58      阅读:48      评论:0      收藏:0      [点我收藏+]

标签:数据   返回结果   ror   bsp   turn   util   except   tostring   system   

 
import com.yemast.roadcqc.server.webserivce.login.request.UserInfoRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.axiom.om.*;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import javax.xml.namespace.QName;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@Slf4j
public class Test { static final String url = "http://localhost:8091/roadcqc_webservice_war_exploded/services/loginIntf?wsdl"; static final String method = "getToken"; static final String targetNamespace = "http://login.webserivce.server.roadcqc.yemast.com"; public static void main(String[] args) { axis2WebService(); } private static void axis2WebService() { try { // 参数值 UserInfoRequest u = new UserInfoRequest(); u.setUploadDateTime("2020-06-01"); u.setUserName("admin"); u.setPassword("admin"); u.setUploadDateTime("2020-01-1"); u.setToken(""); OMElement o = requestWs(targetNamespace,url,method,u); Map m = getResultToMap(o); System.out.println(m.toString()); } catch (Exception ex) { ex.printStackTrace(); } } private static OMElement requestWs(String targetNamespace, String url, String method, Object o) { try { ServiceClient sender = new ServiceClient(); EndpointReference endpointReference = new EndpointReference(url); Options options = new Options(); options.setTo(endpointReference); sender.setOptions(options); OMFactory fac = OMAbstractFactory.getOMFactory(); // 这个和qname差不多,设置命名空间 OMNamespace omNs = fac.createOMNamespace(targetNamespace, ""); //这个是namespace的str OMElement data = fac.createOMElement(method, omNs); //method是方法 QName qname = new QName("request"); //固定的request参数 webservice接收参数的名称 OMElement inner = fac.createOMElement(qname); if (o != null) { //通过反射获取参数 Class c = o.getClass(); Field[] fields = c.getDeclaredFields(); for (Field f : fields) { f.setAccessible(true); QName qname1 = new QName(f.getName()); OMElement inner1 = fac.createOMElement(qname1); inner1.setText(String.valueOf(f.get(o))); inner.addChild(inner1); } } data.addChild(inner); // 发送数据,返回结果 OMElement omElement = sender.sendReceive(data); return omElement; } catch (Exception e) { log.error("ws error:", e); } return null; } //解析返回的结果 public static Map<String, Object> getResultToMap(OMElement element) { Map<String, Object> map = new HashMap<>(); if(element == null ) return map; Iterator iterator = element.getChildElements();//response while (iterator.hasNext()) { //n:return OMElement result = (OMElement) iterator.next(); Iterator iterator_layer2 = result.getChildElements(); while (iterator_layer2.hasNext()) { // 结果 OMElement result_2 = (OMElement) iterator_layer2.next(); String key = result_2.getLocalName(); String value = result_2.getText(); map.put(key, value); } } return map; } }

 

java 调用webservice并解析xml结果

标签:数据   返回结果   ror   bsp   turn   util   except   tostring   system   

原文地址:https://www.cnblogs.com/syscn/p/13157798.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!