Bootstrap

springboot 集成webservice客户端

1、pom.xml依赖

<!-- Apache CXF -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-rs-client</artifactId>
            <version>3.0.0</version>
        </dependency>
<!-- 进行jaxes 服务开发 -->
        <dependency>
          <groupId>org.apache.cxf</groupId>
          <artifactId>cxf-rt-frontend-jaxws</artifactId>
          <version>3.0.0</version>
        </dependency>
<!-- 内置jetty web服务器 -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>3.1.6</version>
        </dependency>

2、调用webservice工具类

package com.example.demo.utils;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

/**
 * https://www.jb51.net/program/31106275g.htm
消费者执行webservice方法时,没有设置超时时间,默认永不超时,然后赶上对端系统出问题,方法就一直执行不完,队列消息也就一直卡住不动。(加try-catch是没有用的,catch不到,就是没有执行完一直卡住,因为超时时间默认永不超时)
 */

public class ClientUtil {

    private static final int timeout = 10000;

    public static String callWebSV(String wsdUrl, String operationName, String... params) throws Exception {
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();

        //设置超时时间 这里有2种方法,不确定到底是哪种,就都写上了
        dcf.getJaxbContextProperties().put("com.sun.xml.ws.request.timeout", timeout);
        dcf.getJaxbContextProperties().put("com.sun.xml.ws.connect.timeout", timeout);
        dcf.getJaxbContextProperties().put("com.sun.xml.internal.ws.connection.timeout", timeout);//建立连接的超时时间为10秒
        dcf.getJaxbContextProperties().put("com.sun.xml.internal.ws.request.timeout", timeout);//指定请求的响应超时时间为10秒

        Client client = dcf.createClient(wsdUrl);

        //这里再设置超时时间好像也行
        //client.getRequestContext().put("com.sun.xml.ws.request.timeout", 10000);
        //client.getRequestContext().put("com.sun.xml.ws.connect.timeout", 10000);
        //client.getRequestContext().put("com.sun.xml.internal.ws.connection.timeout", 10000);//建立连接的超时时间为10秒
        //client.getRequestContext().put("com.sun.xml.internal.ws.request.timeout", 10000);//指定请求的响应超时时间为10秒

        //client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));//设置用户名和密码
        Object[] objects;
        // invoke("方法名",参数1,参数2,参数3....);
        objects = client.invoke(operationName, params);//如果不设置超时时间,那么如果连接不通,就会卡在这一步
        return objects[0].toString();
    }

}

3、xml生成类

package com.example.demo.utils;

import com.example.demo.entity.ReportParam;
import org.apache.tomcat.util.codec.binary.Base64;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import java.io.*;

public class DomUtils {

    /**
     * dom4j生成xml
     * @author Lenovo
     *
     */
    public static String writeXml(ReportParam reportParam) {


                //[1]通过DocumentHelper生成一个Document对象
                Document doc = DocumentHelper.createDocument();

                //链式编程
                //doc.addElement("books").addElement("book").addAttribute("id", "bookone");

                //[2]添加根元素,并返回
        Element request = doc.addElement("request");

        Element reqtype = request.addElement("reqtype");
        reqtype.addText(reportParam.getReqtype());

        Element secret = request.addElement("secret");
        secret.addText("");

        Element body = request.addElement("body");

        Element userid = body.addElement("userid");
        userid.addText(reportParam.getUserid());

        Element seeid = body.addElement("seeid");
        seeid.addText(reportParam.getSeeid());

        Element time = body.addElement("time");
        time.addText(reportParam.getTime());

        Element data = body.addElement("data");

        Element type = data.addElement("type");
        type.addText(reportParam.getType());
        Element value = data.addElement("value");
        value.addText(reportParam.getValue());
        return doc.asXML();
    }

    /**
     * 将图片转换成Base64编码
     * @param path 待处理图片地址
     * @return
     */
    public static String getImgBase(String path) {

        // 将图片文件转化为二进制流
        InputStream in = null;
        byte[] data = null;
        // 读取图片字节数组
        try {
            in = new FileInputStream(path);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 图片头
        //String imghead = "data:image/jpeg;base64,";
        return Base64.encodeBase64String(data);
    }

    public static void writeXmlFile() throws IOException {
        //[1]通过DocumentHelper生成一个Document对象
        Document doc = DocumentHelper.createDocument();

        //链式编程
        //doc.addElement("books").addElement("book").addAttribute("id", "bookone");

        //[2]添加根元素,并返回
        Element root = doc.addElement("ROOT");
        //[5]为book添加子元素
        Element CONS_NO = root.addElement("CONS_NO");
        Element EQUIP_TYPE = root.addElement("EQUIP_TYPE");
        Element FAULT_ATTR = root.addElement("FAULT_ATTR");
        Element FAULT_TYPE = root.addElement("FAULT_TYPE");
        Element WORK_ORDER_NO = root.addElement("WORK_ORDER_NO");
        Element FAULT_INDEX = root.addElement("FAULT_INDEX");
        Element KN_EQUIP_TYPE = root.addElement("KN_EQUIP_TYPE");
        Element TERMINAL_METER_NUM = root.addElement("TERMINAL_METER_NUM");
        Element FAULT_METER_NUM = root.addElement("FAULT_METER_NUM");
        Element FAULT_LEVEL = root.addElement("FAULT_LEVEL");
        Element CP_NO = root.addElement("CP_NO");
        Element CP_NAME = root.addElement("CP_NAME");
        Element HIS_FAULT_NUM = root.addElement("HIS_FAULT_NUM");
        Element CP_ADDR = root.addElement("CP_ADDR");
        Element MR_SECT_NO = root.addElement("MR_SECT_NO");
        Element WARNING_TIME = root.addElement("WARNING_TIME");
        Element OVERDUR_TIME = root.addElement("OVERDUR_TIME");
        Element COLL_MODE = root.addElement("COLL_MODE");
        Element RULE_TYPE = root.addElement("RULE_TYPE");
        Element TMNL_TYPE = root.addElement("TMNL_TYPE");
        Element FAULT_LEVEL2 = root.addElement("FAULT_LEVEL2");
        //[6]为子元素添加文本
        CONS_NO.addText("9110923777");
        EQUIP_TYPE.addText("03");
        FAULT_ATTR.addText("01");
        FAULT_TYPE.addText("1104");
        WORK_ORDER_NO.addText("15A3230310BYS478");
        FAULT_INDEX.addText("4");
        KN_EQUIP_TYPE.addText("05");
        TERMINAL_METER_NUM.addText("1");
        FAULT_METER_NUM.addText("1");
        FAULT_LEVEL.addText("01");
        CP_NO.addText("0012180519");
        CP_NAME.addText("奈曼旗公安局");
        HIS_FAULT_NUM.addText("17");
        CP_ADDR.addText("华明社区111线生态大街");
        MR_SECT_NO.addText("3011649289");
        WARNING_TIME.addText("2023-03-13 10:10:10");
        OVERDUR_TIME.addText("2023-03-15 10:10:10");
        COLL_MODE.addText("27");
        RULE_TYPE.addText("20");
        TMNL_TYPE.addText("01");
        FAULT_LEVEL2.addText("3");
        String s = doc.asXML();

        //[7]将doc输出到xml文件中
//		Writer ww = new FileWriter(new File("src/book2.xml"));
//		doc.write(ww);

        //格式美化实例:
        OutputFormat format = OutputFormat.createPrettyPrint();
        XMLWriter ww = new XMLWriter(new FileWriter(new File("src/book2.xml")),format);
        ww.write(doc);
        ww.close();
    }



}

4、webservice接口调用

//uploadAllData是调用的webservice方法;requertXml调用的webservice方法的参数
String result="";
        try {
            result= ClientUtil.callWebSV(WSDL_URL, "uploadAllData", requertXml);
        } catch (Exception e) {
            log.info("{} 接口调用失败!!!!","uploadAllData");
        }
        log.info("uploadAllData result success {}",result);

;