欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringBoot调用第三方WebService接口的操作技巧(.wsdl与.asmx类型)

程序员文章站 2022-06-25 10:10:30
springboot调webservice接口,一般都会给你url如:http://10.189.200.170:9201/wharfwebservice/services/wharfservice&...

springboot调webservice接口,一般都会给你url如:
http://10.189.200.170:9201/wharfwebservice/services/wharfservice?wsdl
http://10.103.6.158:35555/check_ticket/ticket_check.asmx
其中.asmx是.net开发提供的webservice服务。

依赖

引入相关依赖:

<!-- webservice-->
 <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-web-services</artifactid>
  </dependency>

  <!-- cxf webservice -->
  <dependency>
      <groupid>org.apache.cxf</groupid>
      <artifactid>cxf-spring-boot-starter-jaxws</artifactid>
      <version>3.2.1</version>
  </dependency>
  <dependency>
      <groupid>org.apache.cxf</groupid>
      <artifactid>cxf-rt-transports-http</artifactid>
      <version>3.2.1</version>
  </dependency>

浏览webservice提供的方法,确定入参顺序 直接在浏览器里面访问url,如下

SpringBoot调用第三方WebService接口的操作技巧(.wsdl与.asmx类型)

用soapui工具

SpringBoot调用第三方WebService接口的操作技巧(.wsdl与.asmx类型)

用些是.asmx格式,也可以直接在浏览器访问。会列出方法列表

SpringBoot调用第三方WebService接口的操作技巧(.wsdl与.asmx类型)

代码

创建client:

package com.gqzdev.sctads.configuration;

import com.gqzdev.sctads.constant.commonconstant;
import lombok.extern.slf4j.slf4j;
import org.apache.cxf.endpoint.client;
import org.apache.cxf.jaxws.endpoint.dynamic.jaxwsdynamicclientfactory;
import org.apache.cxf.transport.http.httpconduit;
import org.apache.cxf.transports.http.configuration.httpclientpolicy;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;

/**
 * @author gqzdev
 * @date 2021/08/26 15:53
 **/
@configuration
@slf4j
public class jaxwsclientconfig {

    @bean("jaxwsclient")
    public client client() {
        // 创建动态客户端
        jaxwsdynamicclientfactory clientfactory = jaxwsdynamicclientfactory.newinstance();
		//commonconstant.public_security_url为连接的url,如http://10.189.200.170:9201/wharfwebservice/services/wharfservice?wsdl
        log.info("publicsecurity webservice url : {}", commonconstant.public_security_url);
        //创建client
        client client = clientfactory.createclient(commonconstant.public_security_url);
        httpconduit conduit = (httpconduit) client.getconduit();
        httpclientpolicy policy = new httpclientpolicy();
        policy.setallowchunking(false);
        // 连接服务器超时时间 10秒
        policy.setconnectiontimeout(10000);
        // 等待服务器响应超时时间 20秒
        policy.setreceivetimeout(20000);
        conduit.setclient(policy);
        return client;
    }
}

有了client,便可以直接注入调用invoke。找到自己需要调用的方法:
下面只展示一个方法的调用演示,其他的类似

package com.gqzdev.sctads.service.impl;

import com.gqzdev.sctads.constant.commonconstant;
import com.gqzdev.sctads.service.publicsecurityservice;
import lombok.extern.slf4j.slf4j;
import org.apache.cxf.endpoint.client;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.beans.factory.annotation.qualifier;
import org.springframework.scheduling.annotation.async;
import org.springframework.stereotype.component;

import javax.xml.namespace.qname;

/**
 * @author gqzdev
 * @date 2021/08/26 15:24
 **/
@slf4j
@component
public class publicsecurityserviceimpl implements publicsecurityservice {


    @autowired
    @qualifier("jaxwsclient")
    private client client;

    /**
     * 旅客登记
     */
    @override
//    @async
    public string chineseaddnew(object... params) {
//        qname qname = new qname("service.", commonconstant.chinese_add);
        try {
            object[] invoke = client.invoke(commonconstant.chinese_add, params);
            if (invoke != null && invoke.length >= 1) {
                string result = (string) invoke[0];
                log.info("useraddnew result: {}", result);
                return result;
            }

        } catch (exception e) {
//            e.printstacktrace();
            log.error("invoke ws useraddnew method error: {}", e.getmessage());
        }
        return null;
    }    
}

使用post请求访问webservice接口

package com.gqzdev.sctads.service.impl;

import cn.hutool.core.util.xmlutil;
import com.alibaba.fastjson.json;
import com.alibaba.fastjson.jsonobject;
import lombok.extern.slf4j.slf4j;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.beans.factory.annotation.qualifier;
import org.springframework.http.httpentity;
import org.springframework.http.httpheaders;
import org.springframework.http.mediatype;
import org.springframework.stereotype.component;
import org.springframework.util.linkedmultivaluemap;
import org.springframework.util.multivaluemap;
import org.springframework.web.client.resttemplate;

import java.util.map;

/**
 * 对接票务系统验证
 *
 * @author gqzdev
 * @date 2021/08/25 17:07
 **/
@component
@slf4j
public class ticketcheckserviceimpl implements ticketcheckservice {

    @autowired
    @qualifier("nativeresttemplate")
    private resttemplate resttemplate;

    @override
    public boolean ticketcheck(ticketrequestvo ticketrequestvo) {

        //构造请求参数xml
        map objmap = jsonobject.tojavaobject(jsonobject.parseobject(json.tojsonstring(ticketrequestvo)), map.class);
        string objxml = xmlutil.maptoxmlstr(objmap);
        string requestxml = objxml.replace("<xml>", "<mq_message>").replace("</xml>", "</mq_message>");
        log.info("ticket request params xml: {}", requestxml);

        /**
         * 调用webservice请求
         */
        httpheaders headers = new httpheaders();
        //header参数
        headers.setcontenttype(mediatype.application_form_urlencoded);
        //请求参数
        multivaluemap<string, object> map = new linkedmultivaluemap<>();
        //接口参数
        map.add("msg", requestxml);
        //构造实体对象
        httpentity<multivaluemap<string, object>> param = new httpentity<>(map, headers);
        //发送post请求webservice
        string response = resttemplate.postforobject(commonconstant.ticket_request_url, param, string.class);
        log.info("ticket request response: {}", response);

        /**
         * 解析返回xml,返回是否认证成功
         */
        string responsexml = xmlutil.unescape(response);
        map<string, object> resultmap = xmlutil.xmltomap(responsexml);
        ticketresponsevo ticketresponsevo = jsonutil.map2pojo(resultmap, ticketresponsevo.class);
        //0开闸 ,1不开闸
        if (ticketresponsevo.getmq_message().getmsg_body().getcommand() == 0) {
            return true;
        }
        return false;
    }


}

xml相关操作

关于xml解析处理,这边推荐使用hutool里面的xmlutil

到此这篇关于springboot调用第三方webservice接口(.wsdl与.asmx类型 )的文章就介绍到这了,更多相关springboot webservice接口内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!