Java calls the webservice interface in practice

It is the first time to call this type of interface, and it is still based on the soap mode, so it is more troublesome. Unlike the http interface, you can use third-party tools

The calling code is as follows:

package com.donlim.fms.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.Date;
import java.text.SimpleDateFormat;

/**
 * @ClassName MessageSync
 * @Description message synchronous call function

 * <p>
 * Version 1.0
 **/
@Slf4j
public class MessageSync {

    private static Logger logger = LoggerFactory. getLogger(MessageSync. class);
    private static String destinationApp = "EIP";
    private static String sourceApp = "FMS";
    private static String systemSort = "A12";
    private static String systemName = "FMS";
    private static String ipAddress = "****";
    private static String BO = "to-do notification information synchronization";


    public static int ESBMailBase(String mailID, String billMan, String billUrl,
                                  String mailSubject, String mailBody, int type, String createMan, String mailType
    ) {
        int flag = 0;
        try {
            String operateType = "";
            if (type == 0) {
                operateType = "ADD";
            } else if (type == 1) {
                operateType = "UPDATE";
            } else if (type == 2) {
                operateType = "DELETE";
            }
            Date date = new Date(System. currentTimeMillis());
            String createDate = dateToStr(date);
            //Step 1: create a service address
            URL url = new URL("http://**********:7080/WS/DONLIM/ES/AGENCYNOTICEINFOSYNC?wsdl");
            //Step 2: Open a connection to the service address
            HttpURLConnection connection = (HttpURLConnection) url. openConnection();
            //Step 3: Set parameters
            //3.1 Sending method setting: POST must be capitalized
            connection.setRequestMethod("POST");
            //3.2 Set the data format: content-type
            connection.setRequestProperty("content-type", "text/xml;charset=utf-8");
            //3.3 Set the input and output, because the newly created connection does not have read and write permissions by default,
            connection.setDoInput(true);
            connection.setDoOutput(true);
            //Step 4: organize SOAP data, send request
            String soapXML = getMessageSyncXml(mailID, billMan, billUrl, mailSubject, mailBody, operateType, createDate, createMan, mailType);
            // Send the information as a stream
            OutputStream os = connection. getOutputStream();
            os.write(soapXML.getBytes("UTF-8"));
            //Step 5: Receive server response and print
            int responseCode = connection. getResponseCode();
            System.err.println(responseCode);
            if (200 == responseCode) {
                // Get the data stream returned by the current connection request
                InputStream is = connection. getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                StringBuilder sb = new StringBuilder();
                String temp = null;
                while (null != (temp = br. readLine())) {
                    sb.append(temp);
                }
                // print the result
                System.err.println(sb.toString());
                if (sb.toString().contains("<RCODE>Y</RCODE><ESBCODE>S</ESBCODE>")){
                    flag = 1;
                }
// if (sb.toString().length() == 402 || sb.toString().length() == 406 || sb.toString().length() == 407) {
// flag = 1;
// }
                is. close();
                isr. close();
                br. close();
            }
            os. close();
        } catch (Exception e) {
            log.debug("Failed to send message{}",e.getMessage());

        }
        return flag;
    }


 


    public static String getMessageSyncXml(String mailID, String billMan, String billUrl,
                                           String mailSubject, String mailBody, String type, String createDate, String createMan, String mailType) {
        StringBuilder str = new StringBuilder("<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>" );
        str.append("<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'");
        str.append("xmlns:don='http://www.example.org/DONLIM_ES_AGENCYNOTICEINFOSYNC_086/'>");
        str.append("<soapenv:Header/>");
        str.append("<soapenv:Body>");
        str.append("<don:DONLIM_ES_AGENCYNOTICEINFOSYNC_086>");
        str.append("<SvcHdr>");
        str.append("<SOURCEID>").append(sourceApp).append("</SOURCEID>");
        str.append("<DESTINATIONID>").append(destinationApp).append("</DESTINATIONID>");
        str.append("<TYPE>").append(type).append("</TYPE>");
        str.append("<IPADDRESS>").append(ipAddress).append("</IPADDRESS>");
        str.append("<BO>1</BO>");
        str.append("</SvcHdr>");
        str.append("<AppBody>");
        str.append("<AddNotice>");
        str.append("<Account>").append(billMan).append("</Account>");
        str.append("<CreateDate>").append(createDate).append("</CreateDate>");
        str.append("<CreateMan>").append(createMan).append("</CreateMan>");
        str.append("<MailBody>").append(mailBody).append("</MailBody>");
        str.append("<MailID>").append(mailID).append("</MailID>");
        str.append("<MailSubject>").append(mailSubject).append("</MailSubject>");
        str.append("<MailType>").append(mailType).append("</MailType>");
        str.append("<SystemName>").append(systemName).append("</SystemName>");
        str.append("<SystemSort>").append(systemSort).append("</SystemSort>");
        str.append("<Url>").append(billUrl).append("</Url>");
        str.append("</AddNotice>");
        str.append("</AppBody>");
        str.append("</don:DONLIM_ES_AGENCYNOTICEINFOSYNC_086>");
        str.append("</soapenv:Body>");
        str.append("</soapenv:Envelope>");
        return str.toString();
    }

    public static String dateToStr(Date dateDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        String dateString = formatter. format(dateDate);
        return dateString;
    }

}

Description of returned results: YS

Call code:

MessageSync.ESBMailBase("FMS" + number, approve_id,
                "192.1**.*.**:***", "FMS pending review, order number: " + number + ", please log in to the FMS system to process", "",
                0, "", "to do");

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge Java skill treeHomepageOverview 106937 people are studying systematically