欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java調(diào)用WebService服務(wù)的三種方式總結(jié)

 更新時間:2023年08月24日 10:42:05   作者:364.99°  
雖然WebService這個框架已經(jīng)過時,但是有些公司還在使用,在調(diào)用他們的服務(wù)的時候就不得不面對各種問題,本篇文章總結(jié)了最近我調(diào)用?WebService的心路歷程,3種方式可以分別嘗試,需要的朋友可以參考下

1. HttpClient

依賴:

        <!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- Apache Http httpclient_version-->
        <dependency>
            <groupId>org.apache.httpcomponents.client5</groupId>
            <artifactId>httpclient5</artifactId>
            <version>5.1.3</version>
        </dependency>

代碼:

import lombok.extern.slf4j.Slf4j;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.HttpEntity;
@Slf4j
public class HttpClientUtil {
    /**
     * HttpClient 調(diào)用 WebService
     * @param wsUrl webService地址,格式:http://ip:port/xxx/xxx/soap?wsdl
     * @param json格式的入?yún)?
     * @return
     */
    public static String callServiceHC(String wsUrl, String jsonStr) {
        String xml = createSoapContent(jsonStr);
        String returnDatabase = doPostSoap(wsUrl, xml, "");
        log.info("returnDatabase===>{}", returnDatabase);
        return returnDatabase;
    }
    /**
     * 根據(jù)拼接 xml 字符串
     * @param input
     * @return
     */
    public static String createSoapContent(String jsonStr) {
        log.info("開始拼接請求報文");
        //開始拼接請求報文
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:zys=\"http://www.chenjy.com.cn/\">\n");
        stringBuilder.append("<soapenv:Header/>\n");
        stringBuilder.append("<soapenv:Body>\n");
        stringBuilder.append("<cjy:CallInterface>\n");
        stringBuilder.append("<cjy:msgHeader><![CDATA[\n");
        stringBuilder.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
        stringBuilder.append("<root>\n");
        stringBuilder.append("<serverName>getInfo</serverName>\n");
        stringBuilder.append("<format>xml</format>\n");
        stringBuilder.append("<callOperator>測試</callOperator>\n");
        stringBuilder.append("<certificate>AcsaoP21Lxw5KAoQu6SLs624bhGjwNL0DzxsQ9a7B/HbqNsPPcA==</certificate>\n");
        stringBuilder.append("</root>\n");
        stringBuilder.append("]]></cjy:msgHeader>\n");
        stringBuilder.append("<cjy:msgBody><![CDATA[\n");
        stringBuilder.append( jsonStr+ "\n");
        stringBuilder.append("]]></cjy:msgBody>\n");
        stringBuilder.append("</cjy:CallInterface>\n");
        stringBuilder.append("</soapenv:Body>\n");
        stringBuilder.append("</soapenv:Envelope>");
        log.info("拼接后的參數(shù)"+stringBuilder.toString());
        return stringBuilder.toString();
    }
    /**
     * HTTPClient 調(diào)用 WebService
     * @param url
     * @param soap
     * @param SOAPAction
     * @return
     */
    public static String doPostSoap(String url, String soap, String SOAPAction) {
        //請求體
        String retStr = "";
        // 創(chuàng)建HttpClientBuilder
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        // HttpClient
        CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
        HttpPost httpPost = new HttpPost(url);
        try {
            httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");
            httpPost.setHeader("SOAPAction", SOAPAction);
            StringEntity data = new StringEntity(soap,
                    Charset.forName("UTF-8"));
            httpPost.setEntity(data);
            CloseableHttpResponse response = closeableHttpClient
                    .execute(httpPost);
            HttpEntity httpEntity = response.getEntity();
            if (httpEntity != null) {
                // 打印響應(yīng)內(nèi)容
                retStr = EntityUtils.toString(httpEntity, "UTF-8");
            }
            // 釋放資源
            closeableHttpClient.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return retStr;
    }
}

注意:拼接 xml 字符串的時候要根據(jù)實際的 WebService 地址拼接,可在 soapui 中導(dǎo)入 wsurl 獲取到入?yún)?,如下?/p>

把這些參數(shù)全部拼接進去:

2. Http post

依賴:

        <!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- jodd-http -->
        <dependency>
            <groupId>org.jodd</groupId>
            <artifactId>jodd-http</artifactId>
            <version>6.3.0</version>
        </dependency>

代碼:

import lombok.extern.slf4j.Slf4j;
import jodd.http.*;
public class HttpPostUtil {
    /**
     * http post 調(diào)用 WebService
     * @param wsUrl
     * @param jsonStr
     * @return
     */
    public static String callServiceHP(String wsUrl, String jsonStr) {
        String xml = createSoapContent(jsonStr);
        String uploadFeeDetailJsonStr = postWs(wsUrl, xml);
        return uploadFeeDetailJsonStr;
    }
	/**
     * 根據(jù)拼接 xml 字符串
     * @param input
     * @return
     */
    public static String createSoapContent(String jsonStr) {
        log.info("開始拼接請求報文");
        //開始拼接請求報文
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:zys=\"http://www.chenjy.com.cn/\">\n");
        stringBuilder.append("<soapenv:Header/>\n");
        stringBuilder.append("<soapenv:Body>\n");
        stringBuilder.append("<cjy:CallInterface>\n");
        stringBuilder.append("<cjy:msgHeader><![CDATA[\n");
        stringBuilder.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
        stringBuilder.append("<root>\n");
        stringBuilder.append("<serverName>getInfo</serverName>\n");
        stringBuilder.append("<format>xml</format>\n");
        stringBuilder.append("<callOperator>測試</callOperator>\n");
        stringBuilder.append("<certificate>AcsaoP21Lxw5KAoQu6SLs624bhGjwNL0DzxsQ9a7B/HbqNsPPcA==</certificate>\n");
        stringBuilder.append("</root>\n");
        stringBuilder.append("]]></cjy:msgHeader>\n");
        stringBuilder.append("<cjy:msgBody><![CDATA[\n");
        stringBuilder.append( jsonStr+ "\n");
        stringBuilder.append("]]></cjy:msgBody>\n");
        stringBuilder.append("</cjy:CallInterface>\n");
        stringBuilder.append("</soapenv:Body>\n");
        stringBuilder.append("</soapenv:Envelope>");
        log.info("拼接后的參數(shù)"+stringBuilder.toString());
        return stringBuilder.toString();
    }
    /**
     * 調(diào)用 webService
     * @param url
     * @param jsonStr
     * @return
     */
    public static String postWs(String url, String jsonStr) {
        HttpResponse resp = HttpRequest.post(url).connectionTimeout(60000).timeout(60000)
                .contentType("application/xml", StandardCharsets.UTF_8.toString())
                .header("SOAPAction","")
                .bodyText(jsonStr, "application/xml", "utf-8")
                .charset(StandardCharsets.UTF_8.toString()).trustAllCerts(true)
                .send();
        resp.charset(StandardCharsets.UTF_8.toString());
        return resp.bodyText();
    }
}

3. cxf

Java使用cxf發(fā)布及調(diào)用webservice接口的方法詳解_java_腳本之家 (jb51.net)

到此這篇關(guān)于Java調(diào)用WebService服務(wù)的三種方式分享的文章就介紹到這了,更多相關(guān)Java調(diào)用WebService服務(wù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java多線程 volatile關(guān)鍵字詳解

    Java多線程 volatile關(guān)鍵字詳解

    這篇文章主要介紹了Java多線程 volatile關(guān)鍵字詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-09-09
  • JAVA驗證碼工具實例代碼

    JAVA驗證碼工具實例代碼

    這篇文章主要介紹了JAVA驗證碼工具實例代碼,需要的朋友可以參考下
    2018-03-03
  • 關(guān)于Idea卡在Resolving Maven dependencies的解決方案

    關(guān)于Idea卡在Resolving Maven dependencies的解決方案

    本文詳細(xì)介紹了關(guān)于Idea卡在Resolving Maven dependencies的解決方案,文中通過圖文結(jié)合的形式給大家介紹的非常詳細(xì),對大家解決問題有一定的幫助,需要的朋友可以參考下
    2024-02-02
  • mybatis-parameterType傳入map條件方式

    mybatis-parameterType傳入map條件方式

    這篇文章主要介紹了mybatis-parameterType傳入map條件方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • java之minio文件服務(wù)器的日常操作

    java之minio文件服務(wù)器的日常操作

    本文介紹如何在Java項目中配置Minio服務(wù),通過創(chuàng)建minioConfig和minioDto來管理連接信息,并通過minioUtils工具類實現(xiàn)文件的上傳、下載和刪除功能,詳細(xì)說明了如何從application.yml文件中讀取配置,并強調(diào)了避免static情況下minioDto為null的問題,另外,提到刪除操作是延遲的
    2024-10-10
  • SpringBoot預(yù)防XSS攻擊的實現(xiàn)

    SpringBoot預(yù)防XSS攻擊的實現(xiàn)

    XSS攻擊是一種在web應(yīng)用中的計算機安全漏洞,它允許惡意web用戶將代碼植入到提供給其它用戶使用的頁面,本文主要介紹了SpringBoot預(yù)防XSS攻擊的實現(xiàn),感興趣的可以了解一下
    2023-08-08
  • SpringBoot中的RabbitMQ用法詳解

    SpringBoot中的RabbitMQ用法詳解

    RabbitMQ是一個開源的消息隊列系統(tǒng),它通過AMQP(高級消息隊列協(xié)議)來實現(xiàn)消息的傳遞,SpringBoot是目前非常流行的Java開發(fā)框架,它提供了很多便利性的功能,其中就包括對RabbitMQ的支持,在本文中,我們將介紹如何在SpringBoot中使用RabbitMQ
    2023-07-07
  • 在SpringBoot框架下實現(xiàn)Excel導(dǎo)入導(dǎo)出的方法詳解

    在SpringBoot框架下實現(xiàn)Excel導(dǎo)入導(dǎo)出的方法詳解

    SpringBoot是由Pivotal團隊提供的全新框架,其設(shè)計目的是用來簡化新Spring應(yīng)用的初始搭建以及開發(fā)過程,今天我們就使用純前對按表格控件帶大家了解,如何在Spring Boot框架下實現(xiàn)Excel服務(wù)端導(dǎo)入導(dǎo)出,需要的朋友可以參考下
    2023-06-06
  • SpringBoot如何使用applicationContext.xml配置文件

    SpringBoot如何使用applicationContext.xml配置文件

    這篇文章主要介紹了SpringBoot使用applicationContext.xml配置文件,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • 教你1秒將本地SpringBoot項目jar包部署到Linux環(huán)境(超詳細(xì)!)

    教你1秒將本地SpringBoot項目jar包部署到Linux環(huán)境(超詳細(xì)!)

    spring Boot簡化了Spring應(yīng)用的開發(fā)過程,遵循約定優(yōu)先配置的原則提供了各類開箱即用(out-of-the-box)的框架配置,下面這篇文章主要給大家介紹了關(guān)于1秒將本地SpringBoot項目jar包部署到Linux環(huán)境的相關(guān)資料,超級詳細(xì),需要的朋友可以參考下
    2023-04-04

最新評論