soap方式调用webservice接口

因工作需要对接三方的webservice接口,根据对方提供的WSDL进行开发

1.将WSDL转为java文件,找到所需接口的入参与出参实体复制到本地项目,可直接作为调用的入参与回调的接收实体
可参考 cmd方式转WSDL为java文件

2 根据实体组装请求参数。因为请求的参数一般较为固定,所以这里将这个方法提出来每次传对应参数,主业务代码看起来更清晰。这里的VO就是我们转出来的入参实体

private VO getVO(){
 VO vo = new VO();
  return VO;
 }

3.根据请求参数组装soap xml

  private SOAPMessage createSoapMessage(String queryInterface,String queryBeanName,Object queryBean) throws SOAPException, IOException, IllegalAccessException {
        MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
        SOAPMessage message = messageFactory.createMessage();
        message.setProperty(SOAPMessage.CHARACTER_SET_ENCODING,"utf-8");
        SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
        
         //根据WSDL组装soap xml
        envelope.addNamespaceDeclaration("ser",CommonConstants.TC_TARGET_NAME_SPACE);
        SOAPBody body = envelope.getBody();
        SOAPElement bodyElement = body.addChildElement(queryInterface,"ser");
        SOAPElement queryElement = bodyElement.addChildElement(queryBeanName);

  //通过反射获取参数与值,动态添加到element
        Field[] fields = queryBean.getClass().getDeclaredFields();
        for(Field field : fields){
            field.setAccessible(true);
            Object value = field.get(queryBean);
            if(null != value){
                queryElement.addChildElement(field.getName()).setValue(value.toString());
            }
        }

        System.out.println("\r\n\r\n");
        message.writeTo(System.out);
        System.out.println("\r\n\r\n");
        return message;
    }

4.发送请求 。这里传interfaceName 主要是为了log日志时区分是请求的哪一个接口

  private List<Map<String,Object>> call(String interfaceName,SOAPMessage message) throws SOAPException, TransformerException, DocumentException, IOException {
        SOAPConnectionFactory connectionFactory =  SOAPConnectionFactory.newInstance();
        SOAPConnection connection = connectionFactory.createConnection();
        SOAPMessage response = connection.call(message, CommonConstants.TC_REQUEST_URL);

        Document doc = response.getSOAPPart().getEnvelope().getOwnerDocument();
        StringWriter writer = new StringWriter();
        TransformerFactory.newInstance().newTransformer().transform(new DOMSource(doc),new StreamResult(writer));

        Element root = DocumentHelper.parseText(writer.toString()).getRootElement();

        List<Map<String,Object>> resultMapList = new ArrayList<>();
        parseResult(root,resultMapList);

        //返回结果判断   
        return resultMapList;
    }



    /**
     * 通过根节点递归遍历数据
     * @param root
     */
    private void parseResult(Element root,List<Map<String,Object>> resultMapList) {
        boolean flag =false;
        Map<String, Object> resultMap = new HashMap<String, Object>();
        if (root.elements() != null) {
            List<Element> list = root.elements();
            for (Element e : list) {
                if (e.elements().size() > 0) {
                    parseResult(e,resultMapList);
                }
                if (e.elements().size() == 0) {
                    flag = true;
                    resultMap.put(e.getName(), e.getTextTrim());
                }
            }
        }
        if (flag) {
            resultMapList.add(resultMap);
        }
    }

5.主业务代码处理

你可能感兴趣的:(java,webservice,soap)