当前位置: 代码网 > it编程>编程语言>Java > Java(Springboot)项目调用第三方WebService接口实现代码

Java(Springboot)项目调用第三方WebService接口实现代码

2025年02月25日 Java 我要评论
webservice 简介webservice 接口的发布通常一般都是使用 wsdl(web service descriptive language)文件的样式来发布的,该文档包含了请求的参数信息,

webservice 简介

webservice 接口的发布通常一般都是使用 wsdl(web service descriptive language)文件的样式来发布的,该文档包含了请求的参数信息,返回的结果信息,我们需要根据 wsdl 文档的信息来编写相关的代码进行调用webservice接口。

业务场景描述

目前需要使用 java 调用一个webservice接口,传递参数的数据类型为 xml,返回的也是一个xml的数据类型,需要实现调用接口,获取到xml 之后并解析为 json 格式数据,并返回所需结果给前端。

wsdl 文档

请求地址及方式

接口地址请求方式
http://aabb.balabala.com.cn/services/bd?wsdlsoap

接口请求/响应报文

<?xml version="1.0" encoding="utf-8"?>
<transdata>
  <baseinfo>
    <prjid>bbb</prjid>                  
    <userid>uid</userid>                                
  </baseinfo>
  <inputdata>
    <writetype>220330</writetype>   
    <handcode>8</handcode>                
  </inputdata>
  <outputdata>
    <resultcode>0</resultcode>          
    <resultmsg>获取权限编号成功!</resultmsg>                            
    <orgniseno>shug98456</orgniseno> 
  </outputdata>
</transdata>

代码实现

1、接口请求/响应报文 json 准备

首先准备好所需要的请求参数和返回数据的实体类

(1)transdata

import javax.xml.bind.annotation.xmlaccesstype;
import javax.xml.bind.annotation.xmlaccessortype;
import javax.xml.bind.annotation.xmlelement;
import javax.xml.bind.annotation.xmlrootelement;

@xmlrootelement(name = "transdata")
@xmlaccessortype(xmlaccesstype.field)
public class transdatadto {

    @xmlelement(name = "baseinfo")
    private baseinfodto baseinfo;

    @xmlelement(name = "inputdata")
    private inputdatadto inputdata;

    @xmlelement(name = "outputdata")
    private outputdatadto outputdata;

    public baseinfodto getbaseinfo() {
        return baseinfo;
    }

    public void setbaseinfo(baseinfodto baseinfo) {
        this.baseinfo = baseinfo;
    }

    public inputdatadto getinputdata() {
        return inputdata;
    }

    public void setinputdata(inputdatadto inputdata) {
        this.inputdata = inputdata;
    }

    public outputdatadto getoutputdata() {
        return outputdata;
    }

    public void setoutputdata(outputdatadto outputdata) {
        this.outputdata = outputdata;
    }

}

(2)baseinfo、inputdata、outputdata

baseinfo

import javax.xml.bind.annotation.xmlaccesstype;
import javax.xml.bind.annotation.xmlaccessortype;
import javax.xml.bind.annotation.xmlelement;
import javax.xml.bind.annotation.xmlrootelement;

@xmlrootelement(name = "baseinfo")
@xmlaccessortype(xmlaccesstype.field)
public class baseinfodto {

    @xmlelement(name = "prjid")
    private string prjid;

    @xmlelement(name = "userid")
    private string userid;

    public string getprjid() {
        return prjid;
    }

    public void setprjid(string prjid) {
        this.prjid = prjid;
    }

    public string getuserid() {
        return userid;
    }

    public void setuserid(string userid) {
        this.userid = userid;
    }
}

inputdata

import javax.xml.bind.annotation.xmlaccesstype;
import javax.xml.bind.annotation.xmlaccessortype;
import javax.xml.bind.annotation.xmlelement;
import javax.xml.bind.annotation.xmlrootelement;

@xmlrootelement(name = "inputdata")
@xmlaccessortype(xmlaccesstype.field)
public class inputdatadto {

    @xmlelement(name = "writetype")
    private string writetype;

    @xmlelement(name = "handcode")
    private string handcode;

    public string getwritetype() {
        return writetype;
    }

    public void setwritetype(string writetype) {
        this.writetype = writetype;
    }

    public string gethandcode() {
        return handcode;
    }

    public void sethandcode(string handcode) {
        this.handcode = handcode;
    }
}

outputdata

import javax.xml.bind.annotation.xmlaccesstype;
import javax.xml.bind.annotation.xmlaccessortype;
import javax.xml.bind.annotation.xmlelement;
import javax.xml.bind.annotation.xmlrootelement;

@xmlrootelement(name = "outputdata")
@xmlaccessortype(xmlaccesstype.field)
public class outputdatadto {

    @xmlelement(name = "resultcode")
    private string resultcode;

    @xmlelement(name = "resultmsg")
    private string resultmsg;

    @xmlelement(name = "orgniseno")
    private string orgniseno;

    public string getresultcode() {
        return resultcode;
    }

    public void setresultcode(string resultcode) {
        this.resultcode = resultcode;
    }

    public string getresultmsg() {
        return resultmsg;
    }

    public void setresultmsg(string resultmsg) {
        this.resultmsg = resultmsg;
    }

    public string getorgniseno() {
        return orgniseno;
    }

    public void setorgniseno(string orgniseno) {
        this.orgniseno = orgniseno;
    }
}

2、业务逻辑实现

(1)httpclientbuilder 调用 webservice 接口实现

1.引入 jar 包

  			<!-- jackson core -->
            <dependency>
                <groupid>com.fasterxml.jackson.core</groupid>
                <artifactid>jackson-core</artifactid>
                <version>2.15.0</version>
            </dependency>

            <!-- jackson databind -->
            <dependency>
                <groupid>com.fasterxml.jackson.core</groupid>
                <artifactid>jackson-databind</artifactid>
                <version>2.15.0</version>
            </dependency>

            <!-- jackson dataformat xml -->
            <dependency>
                <groupid>com.fasterxml.jackson.dataformat</groupid>
                <artifactid>jackson-dataformat-xml</artifactid>
                <version>2.15.0</version>
            </dependency>

2.业务逻辑

package com.ruoyi.system.service;

import com.fasterxml.jackson.dataformat.xml.xmlmapper;

import java.io.ioexception;

import com.fasterxml.jackson.core.jsonprocessingexception;
import com.ruoyi.system.dto.soap.baseinfodto;
import com.ruoyi.system.dto.soap.inputdatadto;
import com.ruoyi.system.dto.soap.outputdatadto;
import com.ruoyi.system.dto.soap.transdatadto;
import org.apache.http.httpentity;
import org.apache.http.client.methods.closeablehttpresponse;
import org.apache.http.client.methods.httppost;
import org.apache.http.entity.stringentity;
import org.apache.http.impl.client.closeablehttpclient;
import org.apache.http.impl.client.httpclientbuilder;
import org.apache.http.util.entityutils;
import org.slf4j.logger;
import org.slf4j.loggerfactory;
import org.springframework.stereotype.service;

import java.nio.charset.charset;
import java.util.objects;

@service
public class soaptestservice {

    private static final logger log = loggerfactory.getlogger(soaptestservice.class);

    public string getfinalvalidno(string wsdlurl, string soapacton) {
        string finalvalidno = "";
        transdatadto transdatadto = new transdatadto();
        baseinfodto baseinfodto = new baseinfodto();
        baseinfodto.setprjid("1");
        baseinfodto.setuserid("1");
        inputdatadto inputdatadto = new inputdatadto();
        inputdatadto.sethandcode("1");
        inputdatadto.setwritetype("1");
        transdatadto.setbaseinfo(baseinfodto);
        transdatadto.setinputdata(inputdatadto);
        string soapparam = "";
        try {
            soapparam = transdatadtotoxmlstr(transdatadto);
            string soapresultstr = dosoapcall(wsdlurl, soapparam, soapacton);
            transdatadto transdataresponse = xmltotransdatadto(soapresultstr);
            if (objects.nonnull(transdataresponse)) {
                outputdatadto outputdatadto = transdatadto.getoutputdata();
                if (!"0".equals(outputdatadto.getresultcode())) {
                    log.error("获取权限编号失败,详细信息:{}", outputdatadto.getresultmsg());
                    throw new runtimeexception(outputdatadto.getresultmsg());
                }
                finalvalidno = outputdatadto.getorgniseno();
            }
        } catch (jsonprocessingexception jp) {
            log.error("json 转 xml 异常,详细错误信息:{}", jp.getmessage());
            throw new runtimeexception(jp.getmessage());
        }
        return finalvalidno;
    }


    /**
     * @param wsdlurl:wsdl   地址
     * @param soapparam:soap 请求参数
     * @param soapaction
     * @return
     */
    private string dosoapcall(string wsdlurl, string soapparam, string soapaction) {
        // 返回体
        string responsestr = "";
        // 创建httpclientbuilder
        httpclientbuilder httpclientbuilder = httpclientbuilder.create();
        // httpclient
        closeablehttpclient closeablehttpclient = httpclientbuilder.build();
        httppost httppost = new httppost(wsdlurl);
        try {
            httppost.setheader("content-type", "text/xml;charset=utf-8");
            httppost.setheader("soapaction", soapaction);
            stringentity data = new stringentity(soapparam,
                    charset.forname("utf-8"));
            httppost.setentity(data);
            closeablehttpresponse response = closeablehttpclient
                    .execute(httppost);
            httpentity httpentity = response.getentity();
            if (httpentity != null) {
                // 打印响应内容
                responsestr = entityutils.tostring(httpentity, "utf-8");
                log.info("调用 soap 请求返回结果数据:{}", responsestr);
            }
        } catch (ioexception e) {
            log.error("调用 soap 请求异常,详细错误信息:{}", e.getmessage());
        } finally {
            // 释放资源
            if (closeablehttpclient != null) {
                try {
                    closeablehttpclient.close();
                } catch (ioexception ioe) {
                    log.error("释放资源失败,详细信息:{}", ioe.getmessage());
                }
            }
        }
        return responsestr;
    }


    /**
     * @param transdatadto
     * @return
     * @throws jsonprocessingexception
     * @des json 转 xml 字符串
     */
    private string transdatadtotoxmlstr(transdatadto transdatadto) throws jsonprocessingexception {
        // 将 json 转换为 xml 字符串
        xmlmapper xmlmapper = new xmlmapper();
        stringbuilder stringbuilder = new stringbuilder("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
        stringbuilder.append(xmlmapper.writerwithdefaultprettyprinter().writevalueasstring(transdatadto));
        return stringbuilder.tostring();
    }

    /**
     * @param xmlresponse
     * @return
     * @throws jsonprocessingexception
     * @des xml 转 json
     */
    private transdatadto xmltotransdatadto(string xmlresponse) throws jsonprocessingexception {
        // 将 xml 字符串转换为 java 对象
        xmlmapper xmlmapper = new xmlmapper();
        transdatadto transdatadto = xmlmapper.readvalue(xmlresponse, transdatadto.class);
        return transdatadto;
    }

}

(2)apache axis 方式调用

1.引入依赖

    <dependency>
            <groupid>org.apache.axis</groupid>
            <artifactid>axis</artifactid>
            <version>1.4</version>
    </dependency>

2.业务逻辑

package com.ruoyi.system.service;

import org.apache.axis.client.call;
import org.slf4j.logger;
import org.slf4j.loggerfactory;
import org.springframework.stereotype.component;

import org.apache.axis.client.service;

import java.rmi.remoteexception;

@component
public class apacheaxistestservice {

    private static final logger log = loggerfactory.getlogger(apacheaxistestservice.class);

    public string sendwebservice() {

        string requestxml = "";
        string soapresponsestr = "";
        string wsdlurl = "";
        service service = new service();
        object[] obj = new object[1];
        obj[0] = requestxml;
        log.info("调用soap接口wsdl地址:{},请求参数:{}", wsdlurl, requestxml);
        try {
            call call = (call) service.createcall();
            call.settargetendpointaddress(wsdlurl);
            call.settimeout(integer.valueof(30000));
            call.setoperation("doservice");
            soapresponsestr = (string) call.invoke(obj);
        } catch (remoteexception r) {
            log.error("调用 soap 接口失败,详细错误信息:{}", r.getmessage());
            throw new runtimeexception(r.getmessage());
        }
        return soapresponsestr;
    }
}

注意!!!!!!!如果现在开发webservice,用的大多是axis2或者cxf。

有时候三方给的接口例子中会用到标题上面的类,这个在axis2中是不存在,这两个类属于axis1中的!!!

总结 

到此这篇关于java(springboot)项目调用第三方webservice接口实现代码的文章就介绍到这了,更多相关springboot调用第三方webservice接口内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com